react hoc

作者: 郑无穷大 | 来源:发表于2018-10-26 11:19 被阅读0次
import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';


class InputComponent extends Component {
    componentWillReceiveProps(){
    }
    render() {
      return <div>{this.props.name}</div>
    }
}
// function logProps (InputComponent) {
//     InputComponent.prototype.componentWillReceiveProps = function(nextProps){
//         console.log('current props' , this.props)
//         console.log('next props' , nextProps)
//     };
//     //污染原型链,不推荐
//     return InputComponent;
// }
function logProps (WrappedComponent) {
    return class extends Component {
        componentWillReceiveProps(nextProps) {
            console.log('current props' , this.props)
            console.log('next props' , nextProps)
        }
        render() {
            return <WrappedComponent {...this.props} />;
        }
    }
}
const EnhancedComponent = logProps(InputComponent)
let name = 'xxx'
setInterval(()=>{
    name += 'hahaha'
    ReactDOM.render(<EnhancedComponent name={name}/>, document.getElementById('root'));
},2000)

serviceWorker.unregister();

高阶函数对组件的封装,譬如多个组件大部分相同,只是需要局部变化,可通过高阶函数来进行封装处理。

相关文章

  • react 滚动到顶部组件

    核心代码 react HOC

  • 从高阶函数到高阶组件

    介绍 高阶组件(HOC)是 React 中用于复用组件逻辑的一种高级技巧。HOC 自身不是 React API 的...

  • React 学习笔记二 - HOC 高阶组件理解

    官方定义 高阶组件(HOC)是 React 中用于 复用组件逻辑 的一种高级技巧。HOC 自身不是 React A...

  • 高阶组件简介

    一、定义 高阶组件(HOC)是 React 中用于复用组件逻辑的一种高级技巧。HOC 自身不是 React API...

  • React 进阶之高阶组件

    高阶组件 HOC 高阶组件(HOC)是react中的高级技术,用来重用组件逻辑。但高阶组件本身并不是React A...

  • React - 高阶组件

    高阶组件(HOC)是 React 中用于复用组件逻辑的一种高级技巧。HOC 自身不是 React API 的一部分...

  • react hoc

    高阶函数对组件的封装,譬如多个组件大部分相同,只是需要局部变化,可通过高阶函数来进行封装处理。

  • React HOC

    在React官网文档学习React HOC,整个看了一遍还是云里雾里的,于是按照官网文档,自己动手实践一下。官网地...

  • react hoc

    一、概念 高阶组件的概念应该是来源于JavaScript的高阶函数:高阶函数就是接受函数作为输入或者输出的函数 高...

  • 高阶组件

    一、高阶组件(HOC)是React 中用于复用组件逻辑的一种高级技巧。HOC自身不是React API的一部分,它...

网友评论

      本文标题:react hoc

      本文链接:https://www.haomeiwen.com/subject/otfhtqtx.html