zoukankan      html  css  js  c++  java
  • 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);
    
    每天进步一点点
  • 相关阅读:
    国内外手机号码正则表达式
    apt安装Neo4j
    经典决策树模型
    自动文档摘要评价方法
    scrapy-splash解析javascript
    ubuntu安装splash
    iptables的删除命令中的相关问题
    ARTS第七周
    ARTS第六周
    ARTS第五周
  • 原文地址:https://www.cnblogs.com/smallstars/p/14074864.html
Copyright © 2011-2022 走看看