• React 学习(九) HOC增强组件(函数)


    Higher-Order Components (HOC)

    HOC is a function with arguments as component and return value as new function

    // index.js
    ReactDOM.render(<App name="Smallstars" />, document.getElementById("root"));
    
    // Components
    import React, { PureComponent } from "react";
    
    class App extends PureComponent {
      render() {
        return (
          <div>
            App:
            {this.props.name}
          </div>
        );
      }
    }
    
    const EnhanceComponent = (WrappedComponent) => {
      class NewComponent extends PureComponent {
        render() {
          return <WrappedComponent {...this.props} />;
        }
      }
    
      // Change the display name of Components
      NewComponent.dispalyName = "StarsComponents";
      return NewComponent;
    };
    
    const EnhanceComponent2 = (WrappedComponent) => {
      function NewComponent(props) {
        return <WrappedComponent {...props} />;
      }
    
      // Change the display name of Components
      return NewComponent;
    };
    
    export default EnhanceComponent(App);
    
    每天进步一点点
  • 相关阅读:
    php apc缓存以及与redis的对比
    React Refs
    React 表单与事件
    React AJAX
    React 组件生命周期
    React 组件 API
    React Props
    React State(状态)
    react 组件之间传值
    react 创建组件
  • 原文地址:https://www.cnblogs.com/smallstars/p/14074864.html
Copyright © 2020-2023  润新知