首页 技术 正文
技术 2022年11月7日
0 收藏 924 点赞 540 浏览 3281 个字

定义:高阶组件就是一个函数,且该函数接受一个组件作为参数,并返回一个新的组件。

A higher-order component is a function that takes a component and returns a new component.

函数模拟高阶组件

  1. 最普通的方法,一个welcome,一个goodbye。两个函数先从localStorage读取了username,然后对username做了一些处理。
function welcome() {
let username = localStorage.getItem('username');
console.log('welcome ' + username);
}function goodbey() {
let username = localStorage.getItem('username');
console.log('goodbey ' + username);
}welcome();
goodbey();

我们发现两个函数有一句代码是一样的,这叫冗余。

我们要写一个中间函数,读取username,他来负责把username传递给两个函数。

function welcome(username) {
console.log('welcome ' + username);
}function goodbey(username) {
console.log('goodbey ' + username);
}function wrapWithUsername(wrappedFunc) {
let newFunc = () => {
let username = localStorage.getItem('username');
wrappedFunc(username);
};
return newFunc;
}welcome = wrapWithUsername(welcome);
goodbey = wrapWithUsername(goodbey);welcome();
goodbey();

好了,我们里面的wrapWithUsername函数就是一个“高阶函数”。
他做了什么?他帮我们处理了username,传递给目标函数。我们调用最终的函数welcome的时候,根本不用关心username是怎么来的。

我们增加个用户study函数。

function study(username){
console.log(username+' study');
}
study = wrapWithUsername(study);study();

这里你是不是理解了为什么说wrapWithUsername是高阶函数?我们只需要知道,用wrapWithUsername包装我们的study函数后,study函数第一个参数是username

我们写平时写代码的时候,不用关心wrapWithUsername内部是如何实现的。

高阶组件

高阶组件就是一个没有副作用的纯函数。

我们把上一节的函数统统改成react组件。

1、最普通的组件

welcome函数转为react组件。

import React, {Component} from 'react'class Welcome extends Component {
constructor(props) {
super(props);
this.state = {
username: ''
}
} componentWillMount() {
let username = localStorage.getItem('username');
this.setState({
username: username
})
} render() {
return (
<div>welcome {this.state.username}</div>
)
}
}export default Welcome;

goodbey函数转为react组件。

import React, {Component} from 'react'class Goodbye extends Component {
constructor(props) {
super(props);
this.state = {
username: ''
}
} componentWillMount() {
let username = localStorage.getItem('username');
this.setState({
username: username
})
} render() {
return (
<div>goodbye {this.state.username}</div>
)
}
}export default Goodbye;
  1. 按照上一节wrapWithUsername函数的思路,我们来写一个高阶组件(高阶组件就是一个函数,且该函数接受一个组件作为参数,并返回一个新的组件)。
import React, {Component} from 'react'export default (WrappedComponent) => {
class NewComponent extends Component {
constructor() {
super();
this.state = {
username: ''
}
} componentWillMount() {
let username = localStorage.getItem('username');
this.setState({
username: username
})
} render() {
return <WrappedComponent username={this.state.username}/>
}
} return NewComponent
}

这样我们就能简化Welcome组件和Goodbye组件。

import React, {Component} from 'react';
import wrapWithUsername from 'wrapWithUsername';class Welcome extends Component { render() {
return (
<div>welcome {this.props.username}</div>
)
}
}Welcome = wrapWithUsername(Welcome);export default Welcome;
import React, {Component} from 'react';
import wrapWithUsername from 'wrapWithUsername';class Goodbye extends Component { render() {
return (
<div>goodbye {this.props.username}</div>
)
}
}Goodbye = wrapWithUsername(Goodbye);export default Goodbye;

高阶组件就是把username通过props传递给目标组件了。目标组件只管从props里面拿来用就好了。

到这里位置,高阶组件就讲完了。你再返回去理解下定义,是不是豁然开朗~

你现在理解react-reduxconnect函数~

reduxstateaction创建函数,通过props注入给了Component
你在目标组件Component里面可以直接用this.props去调用redux stateaction创建函数了。

ConnectedComment = connect(mapStateToProps, mapDispatchToProps)(Component);

相当于这样

// connect是一个返回函数的函数(就是个高阶函数)
const enhance = connect(mapStateToProps, mapDispatchToProps);
// 返回的函数就是一个高阶组件,该高阶组件返回一个与Redux store
// 关联起来的新组件
const ConnectedComment = enhance(Component);

antd的Form也是一样的

const WrappedNormalLoginForm = Form.create()(NormalLoginForm);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287