antd里面的form表单方面,遇到一个高阶函数,以及高阶组件,于是看了一下这方面内容,前辈们的文章写得也非常详细,这里就稍微kobe一下

高阶函数与高阶组件

高阶函数:

高阶函数,是一种特别的函数,接受的参数为函数,返回值也是函数

成立条件,二者兼一即可

    1).一类特别的函数
a).接受函数类型的参数
b).函数返回值是函数

常见的高阶函数:

    2).常见
a).定时器:setTimeout()/setInterval()
b).Promise:Promise(()=>{}) then(value=>{},reason=>{})
c).数组遍历相关的方法: forEach()/ filter()/ map()/ find()/ findindex()
d).fn.bind() 本身是个函数,bind方法返回一个新的函数方法
e).Form.create()() create函数能够包装组件,生成另外一个组件的新功能函数
f).getFieldDecorator()()
1)函数作为参数的高阶函数
setTimeout(()=>{
console.log("aaaa")
},1000)
//2 函数作为返回值输出的高阶函数
function foo(x){
return function(){
return x
}
} //平时遇到的应用场景
//ajax中
$.get("/api",function(){
console.log("获取成功")
}) //数组中
some(), every(),filter(), map()和forEach()

高阶组件

1 高阶组件就是接受一个组件作为参数并返回一个新组件的函数

2 高阶组件是一个函数,并不一个组件

简单说:高阶组件(函数)就好比一个加工厂,同样的配件、外壳、电池..工厂组装完成就是苹果手机,华为手机组装完成就是华为手机,基本材料都是相同,不同工厂(高阶组件)有不同的实现及产出。当然这个工厂(高阶组件)也可能是针对某个基本材料的处理,总之产出的结果拥有了输入组件不具备的功能,输入的组件可以是一个组件的实例,也可以是一个组件类,还可以是一个无状态组件的函数

解决什么问题?

随着项目越来越复杂,开发过程中,多个组件需要某个功能,而且这个功能和页面并没有关系,所以也不能简单的抽取成一个新的组件,但是如果让同样的逻辑在各个组件里各自实现,无疑会导致重复的代码。比如页面有三种弹窗一个有title,一个没有,一个又有右上角关闭按钮,除此之外别无它样,你总不能整好几个弹窗组件吧,这里除了tilte,关闭按钮其他的就可以做为上面说的基本材料。

高阶组件总共分为两大类

  • 代理方式
    1. 操纵prop
    2. 访问ref(不推荐)
    3. 抽取状态
    4. 包装组件
  • 继承方式
    1. 操纵生命周期
    2. 操纵prop

代理方式之 操纵prop

删除prop
import React from 'react'
function HocRemoveProp(WrappedComponent) {
return class WrappingComPonent extends React.Component {
render() {
const { user, ...otherProps } = this.props;
return <WrappedComponent {...otherProps} />
}
}
}
export default HocRemoveProp;

增加prop

import React from 'react'

const HocAddProp = (WrappedComponent,uid) =>
class extends React.Component {
render() {
const newProps = {
uid,
};
return <WrappedComponent {...this.props} {...newProps} />
}
} export default HocAddProp;

上面HocRemoveProp高阶组件中,所做的事情和输入组件WrappedComponent功能一样,只是忽略了名为user的prop。也就是说,如果WrappedComponent能处理名为user的prop,这个高阶组件返回的组件则完全无视这个prop。

const { user, ...otherProps } = this.props;

这是一个利用es6语法技巧,经过上面的语句,otherProps里面就有this.props中所有的字段除了user.
假如我们现在不希望某个组件接收user的prop,那么我们就不要直接使用这个组件,而是把这个组件作为参数传递给HocRemoveProp,然后我们把这个函数的返回结果当作组件来使用
两个高阶组件的使用方法:

const  newComponent = HocRemoveProp(SampleComponent);
const newComponent = HocAddProp(SampleComponent,'1111111');

也可以利用decorator语法糖这样使用:

import React, { Component } from 'React';

@HocRemoveProp
class SampleComponent extends Component {
render() {}
}
export default SampleComponent;
//例子: A组件里面包含B组件

 import React , { Component }from 'react'
function A(WrappedComponent){
return class A extends Component{ //这里必须retrun出去
render() {
return(
<div>
这是A组件
<WrappedComponent></WrappedComponent>
</div>
)
}
}
} export default A

高阶组件应用:

//传参数

import React, { Component } from 'react';
import './App.css';
import B from './components/B'
class App extends Component {
render() {
return (
<div className="App">
这是我的APP
<B age="18" name="Tom"/>
</div>
);
}
}
export default App; //A组件
import React , { Component }from 'react'
export default (title)=> WrappedComponent => {
  return class A extends Component{
   render() {
   return(
   <div>
   这是A组件{title}
   <WrappedComponent sex="男" {...this.props}></WrappedComponent>
   </div>
   )
   }
   }
 } //B组件
import React , { Component }from 'react'
import A from './A.js'
class B extends Component{
render() {
return(
<div>
性别:{this.props.sex}
年龄:{this.props.age}
姓名:{this.props.name}
</div>
)
}
}
export default A('提示')(B) //有两种方式引用高阶函数,第一种入上
//第二种 import React , { Component }from 'react'
import a from './A.js'
@a('提示')
class B extends Component{
render() {
return(
<div>
性别:{this.props.sex}
年龄:{this.props.age}
姓名:{this.props.name}
</div>
)
}
}
export default B

使用高阶组件

1.higherOrderComponent(WrappedComponent);
2.@higherOrderComponent

高阶组件应用

1.代理方式的高阶组件
返回的新组件类直接继承自React.Component类,新组件扮演的角色传入参数组件的一个代理,
在新组件的render函数中,将被包裹组件渲染出来,除了高阶组件自己要做的工作,其余功能全都转手给了被包裹的组件 2.继承方式的高阶组件
采用继承关联作为参数的组件和返回的组件,假如传入的组件参数是WrappedComponent,那么返回的组件就直接继承自WrappedComponent
//代理方式的高阶组件
export default ()=> WrappedComponent =>
class A extends Component {
render(){
const { ...otherProps } = this.props;
return <WrappedComponent {...otherProps} />
}
} //继承方式的高阶组件
export default () => WrappedComponent =>
class A extends WrappedComponent {
render(){
const { use,...otherProps } = this.props;
this.props = otherProps;
return super.render()
}
}

继承方式高阶组件的实现

   //D.js
import React from 'react'
const modifyPropsHOC= (WrappedComponent) => class NewComponent extends WrappedComponent{
render() {
const element = super.render();
const newStyle = {
color: element.type == 'div'?'red':'green'
}
const newProps = {...this.props,style:newStyle}
return React.cloneElement(element,newProps,element.props.children)
}
}
export default modifyPropsHOC // E.js import React, {Component} from 'react'
import D from './D'
class E extends Component {
render(){
return (
<div>
我的div
</div>
);
}
} export default D(E) // F.js
import React, {Component} from 'react'
import d from './D'
class F extends Component {
render(){
return (
<p>
我的p
</p>
);
}
} export default d(F) import React, { Component } from 'react';
import './App.css';
import E from './components/E'
import F from './components/F'
class App extends Component {
render() {
return (
<div className="App">
这是我的APP
<E></E>
<F></F>
</div>
);
}
} export default App;

修改生命周期

import React from 'react'
const modifyPropsHOC= (WrappedComponent) => class NewComponent extends WrappedComponent{
componentWillMount(){
alert("我的修改后的生命周期");
}
render() {
const element = super.render();
const newStyle = {
color: element.type == 'div'?'red':'green'
}
const newProps = {...this.props,style:newStyle}
return React.cloneElement(element,newProps,element.props.children)
}
} export default modifyPropsHOC

高阶组件&&高阶函数(一)的更多相关文章

  1. React 精要面试题讲解(五) 高阶组件真解

    说明与目录 在学习本章内容之前,最好是具备react中'插槽(children)'及'组合与继承' 这两点的知识积累. 详情请参照React 精要面试题讲解(四) 组合与继承不得不说的秘密. 哦不好意 ...

  2. 关于react16.4——高阶组件(HOC)

    高阶组件是react中用于重用组件逻辑的高级技术.可以说是一种模式.具体来说呢,高阶组件是一个函数,它接收一个组件并返回一个新的组件. 就像这样, const EnhancedComponent = ...

  3. React中的高阶组件,无状态组件,PureComponent

    1. 高阶组件 React中的高阶组件是一个函数,不是一个组件. 函数的入参有一个React组件和一些参数,返回值是一个包装后的React组件.相当于将输入的React组件进行了一些增强.React的 ...

  4. 高阶组件(Higher-Order Components)

    有时候人们很喜欢造一些名字很吓人的名词,让人一听这个名词就觉得自己不可能学会,从而让人望而却步.但是其实这些名词背后所代表的东西其实很简单. 我不能说高阶组件就是这么一个东西.但是它是一个概念上很简单 ...

  5. 修饰器&高阶组件

    一.修饰器 1.类的修饰 修饰器是一个函数,用来修改类的行为 function testable(target) { target.isTestable = true; } @testable cla ...

  6. React 高阶组件浅析

    高阶组件的这种写法的诞生来自于社区的实践,目的是解决一些交叉问题(Cross-Cutting Concerns).而最早时候 React 官方给出的解决方案是使用 mixin .而 React 也在官 ...

  7. 8、react 高阶组件

    1.高阶组件:封装 高阶组件使用得是react得一种模式,增强现有组件得功能 一个高阶组件就是一个函数,这个函数接收得是组件类作为参数得,并且返回得是一个新组件,再返回得新组件中有输入参数组件不具备得 ...

  8. react第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件)

    第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件) #受控组件 简而言之,就是受到状态state控制的表单,表单的值改变则state值也改变,受控组件必须要搭配onc ...

  9. react高阶组件的使用

    为了提高代码的复用在react中我们可以使用高阶组件 1.添加高阶组件 高阶组件主要代码模板HOC.js export default (WrappedComponent) => { retur ...

随机推荐

  1. 201871010119-帖佼佼《面向对象程序设计(java)》第一周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接>  https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址>   ...

  2. 3年java开发竟然还不知道Lambda的这个坑

    背景 有朋友反馈zk连接很慢.整理出zk连接的关键逻辑如下: 上面的代码造成第一次调用ClientZkAgent.getInstance的时候,需耗时10s, 这个时间恰好跟semaphore的超时时 ...

  3. python学习-文件创建读取

    # 文件创建 # 读写# 文件存在?不存在?在操作系统上# 读 read r 写 write w# 打开一个文件# fs = open("xiaojian.txt",encodin ...

  4. 《一头扎进》系列之Python+Selenium框架设计篇4- 价值好几K的框架,呵!这个框架有点意思啊

    1.简介 前面文章,我们实现了框架的一部分功能,包括日志类和浏览器引擎类的封装,今天我们继续封装一个基类和介绍如何实现POM.关于基类,是这样定义的:把一些常见的页面操作的selenium封装到bas ...

  5. CCF-CSP题解 201903-4 消息传递接口

    求并行的各个进程,且进程内部顺序执行的情况下,会不会出现"死锁". 首先用\(%[^\n]\)将每个进程读入.最后过不了居然是因为\(str[\ ]\)开小了(悲喜交加.存储在\( ...

  6. django基础之day04,聚合查询和分组查询

    聚合查询: 聚合函数必须用在分组之后,没有分组其实默认整体就是一组 Max Min Sum Avg Count 1.分组的关键字是:aggretate 2.导入模块 from django.db.mo ...

  7. java之构造器

    1.构造方法的作用:在new创建对象时为其赋值. 2.构造方法的分类: ①无参构造public 同类名(){},有参构造public 同类名(参数列表){语句}. ②构造方法没有方法名,没有返回值类型 ...

  8. df,dh 命令

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/df2f05c4-b ...

  9. 【CV现状-2】三维感知

    #磨染的初心--计算机视觉的现状 [这一系列文章是关于计算机视觉的反思,希望能引起一些人的共鸣.可以随意传播,随意喷.所涉及的内容过多,将按如下内容划分章节.已经完成的会逐渐加上链接.] 缘起 三维感 ...

  10. Spring Boot 2.X 国际化

    国际化文件的编写 messages.properties init project 7月前 messages_en_US.properties init project 7月前 messages_zh ...