高阶组件&&高阶函数(一)
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,关闭按钮其他的就可以做为上面说的基本材料。
高阶组件总共分为两大类
- 代理方式
- 操纵prop
- 访问ref(不推荐)
- 抽取状态
- 包装组件
- 继承方式
- 操纵生命周期
- 操纵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
高阶组件&&高阶函数(一)的更多相关文章
- React 精要面试题讲解(五) 高阶组件真解
说明与目录 在学习本章内容之前,最好是具备react中'插槽(children)'及'组合与继承' 这两点的知识积累. 详情请参照React 精要面试题讲解(四) 组合与继承不得不说的秘密. 哦不好意 ...
- 关于react16.4——高阶组件(HOC)
高阶组件是react中用于重用组件逻辑的高级技术.可以说是一种模式.具体来说呢,高阶组件是一个函数,它接收一个组件并返回一个新的组件. 就像这样, const EnhancedComponent = ...
- React中的高阶组件,无状态组件,PureComponent
1. 高阶组件 React中的高阶组件是一个函数,不是一个组件. 函数的入参有一个React组件和一些参数,返回值是一个包装后的React组件.相当于将输入的React组件进行了一些增强.React的 ...
- 高阶组件(Higher-Order Components)
有时候人们很喜欢造一些名字很吓人的名词,让人一听这个名词就觉得自己不可能学会,从而让人望而却步.但是其实这些名词背后所代表的东西其实很简单. 我不能说高阶组件就是这么一个东西.但是它是一个概念上很简单 ...
- 修饰器&高阶组件
一.修饰器 1.类的修饰 修饰器是一个函数,用来修改类的行为 function testable(target) { target.isTestable = true; } @testable cla ...
- React 高阶组件浅析
高阶组件的这种写法的诞生来自于社区的实践,目的是解决一些交叉问题(Cross-Cutting Concerns).而最早时候 React 官方给出的解决方案是使用 mixin .而 React 也在官 ...
- 8、react 高阶组件
1.高阶组件:封装 高阶组件使用得是react得一种模式,增强现有组件得功能 一个高阶组件就是一个函数,这个函数接收得是组件类作为参数得,并且返回得是一个新组件,再返回得新组件中有输入参数组件不具备得 ...
- react第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件)
第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件) #受控组件 简而言之,就是受到状态state控制的表单,表单的值改变则state值也改变,受控组件必须要搭配onc ...
- react高阶组件的使用
为了提高代码的复用在react中我们可以使用高阶组件 1.添加高阶组件 高阶组件主要代码模板HOC.js export default (WrappedComponent) => { retur ...
随机推荐
- Python发送邮件以及对其封装
对Python发送邮件进行封装 Python发送邮件分为四步 连接到smtp服务器 登陆smtp服务器 准备邮件 发送邮件 导入所需要的包 import smtplib from email.mime ...
- 【Nodejs】392- 基于阿里云的 Node.js 稳定性实践
前言 如果你看过 2018 Node.js 的用户报告,你会发现 Node.js 的使用有了进一步的增长,同时也出现了一些新的趋势. Node.js 的开发者更多的开始使用容器并积极的拥抱 Serve ...
- 【Oracle】ORA-12560: TNS: 协议适配器错误
问题现象: ORA-12560: TNS: 协议适配器错误 解决方法: 启动监听服务
- ruby 使用 rqrcode 生成二维码
参考: https://github.com/whomwah/rqrcode 1. gem 'rqrcode' 2. 在helper中: require 'base64' def generat ...
- 真伪随机数 ——Random和SecureRandom
Random Random用来创建伪随机数.所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的. 要生成一个随机数,可以使用nextInt().nextLong().nextFlo ...
- .NET Core Razor Pages中ajax get和post的使用
ASP.NET Core Razor Pages Web项目大部分情况下使用继承与PageModel中的方法直接调用就可以(asp-page),但是有些时候需要使用ajax调用,更方便些.那么如何使用 ...
- abp示例项目BookStore搭建部署
之前部署过BookStore项目,但是换了新电脑也想好好学习下这个示例项目,于是在新电脑上重新拉了Git上的ABP项目代码,一编译生成BookStore项目就报错,可以参考 abp示例项目BookSt ...
- Hack the Breach 2.1 VM (CTF Challenge)
主机扫描: ╰─ nmap -p- -A 192.168.110.151Starting Nmap 7.70 ( https://nmap.org ) at 2019-08-29 09:48 CSTN ...
- 解决:install service error: InstallAsEventCreate() failed: xxx registry key already exists
目录 一.事故现场 二.分析 三.解决方案 一.事故现场 在windows系统下安装服务,cmd窗口执行如下命令: "E:\work\_base\PsUm\PsUser\LdapWebSer ...
- Centos 中使用通过docker 部署.netcore
此前,我一直觉得,目前网络上的各种各样的技术文章.技术文档已经足够多,任何一种技术,都可以或多或少的在网络上找到教程,或者在qq群里找到前辈解答.所以,我觉得自己在博客上写文章的意义甚少.甚至觉得自己 ...