Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deep into react 之 高阶组件 #51

Open
ihtml5 opened this issue Mar 1, 2017 · 0 comments
Open

deep into react 之 高阶组件 #51

ihtml5 opened this issue Mar 1, 2017 · 0 comments

Comments

@ihtml5
Copy link
Owner

ihtml5 commented Mar 1, 2017

前言

高阶组件类似于函数式编程中的高阶函数,指的是向函数中传递一个react组件,函数将返回一个新的react组件。使用高阶组件我们可以很轻松地对原有组件进行进一步的抽象并同时保持原有组件的稳定性。

高阶组件的特性体现在两个地方:

  1. 属性代理
  2. 反向继承

属性代理

import React, { Component } from 'react';
const EnchanceComFactory = (wrappedComponent) => {
     return class extends Component {
         const newProps = {
               text: 'just do it'
          };
         render() {
             //  React.createElement(WrappedComponent, this.props, null)
              return <wrappedComponent {...this.props} {...newProps}/> 
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant