HOC高阶组件

258 阅读1分钟
import React, { Component } from 'react';
//高阶组件:不是组件,本质上是一个函数,这个函数它接收一个组件或者多个组件,返回一个新的组件,Comment为高阶组件
//高阶函数
//定义:接收的参数是函数或者返回值是函数
//常见的:数组遍历的相关方法、定时器、promise、高阶组件
//作用:实现一个更加强大的动态功能
// const highOrderCom = (Comp)=>{
// // 返回一个新组件
// const NewComponent = (props)=>{
// //属性代理
// const attr = {type:'高阶组件',price:'1999'}
// return <Comp {...props} {...attr}></Comp>
// }
// return NewComponent
// }

const highOrderCom = (Comp)=>{
return class extends Component{
constructor(props){
super(props);
}
componentDidMount(){
console.log('发起ajax请求')
}
render(){
return(
<Comp{...this.props} name='react' content='高阶组件的使用'></Comp>
)
}
}
}
class Hoc extends Component {
render() {
return (
<div>
<h3>当前课程:{this.props.name}</h3>
<h3>当前价格:{this.props.content}</h3>
</div>
);
}
}

export default highOrderCom(Hoc);