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

高阶组件 (HOC) #20

Open
oliver1204 opened this issue Feb 19, 2019 · 0 comments
Open

高阶组件 (HOC) #20

oliver1204 opened this issue Feb 19, 2019 · 0 comments

Comments

@oliver1204
Copy link
Owner

oliver1204 commented Feb 19, 2019

高阶组件

定义

高阶组件是一个函数,能够接受一个简单的组件并返回一个新的组件,高阶组件就是一个没有副作用的纯函数。

高阶组件有以下特点:

  • 传入一个组件
  • 返回一个新组件
  • 是一个函数
const EnhancedComponent = higherOrderComponent(WrappedComponent);

组件是将props转化成UI,然而高阶组件将一个组价转化成另外一个组件。

实现方式有以下

实现高阶组件的方式有以下两种:

  1. 属性代理(Props Proxy)
    • 操作props
    • 获得refs的引用
    • 抽象state
    • 用其他元素包裹组件
  2. 反向继承(Inheritance Inversion)
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