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

你好,这块儿的写法没看懂,求大神赐教。。 #19

Open
fangzongwu opened this issue Sep 6, 2017 · 2 comments
Open

你好,这块儿的写法没看懂,求大神赐教。。 #19

fangzongwu opened this issue Sep 6, 2017 · 2 comments

Comments

@fangzongwu
Copy link

**const NetInfoDecorator = WrappedComponent => class extends Component {** constructor(props) { super(props) this.state = { isConnected: true, } }
是在函数中返回一个组件吗?把组件的名字作为参数?

@ljunb
Copy link
Owner

ljunb commented Sep 6, 2017

@fangzongwu 你好,这里是高阶组件的写法,类似于高阶函数。

JS中可以把函数作为参数,传入到另一个函数中,比如我们常用的通过map去渲染多个相同组件的时候,这里map就是这种应用(可以延伸学习函数式编程)。

高阶组件与此类似,就是可以传入一个组件,对该组件做一些自己的加工,然后返回加工后的组件。比如这个NetInfoDecorator,就是为传入的组件添加了网络状态的监听,并把网络连接标志位 isConnected作为props,传递到传入的组件去。

当然这里应用不一定准确,只是为了练习这种写法,什么时候要用高阶组件,还要看你实际情况。另外推荐《深入React技术栈》,这本书也是比较深入地讲解了这些内容的~

@allenxieyusheng
Copy link

加油

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants