We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
**const NetInfoDecorator = WrappedComponent => class extends Component {** constructor(props) { super(props) this.state = { isConnected: true, } } 是在函数中返回一个组件吗?把组件的名字作为参数?
**const NetInfoDecorator = WrappedComponent => class extends Component {** constructor(props) { super(props) this.state = { isConnected: true, } }
The text was updated successfully, but these errors were encountered:
@fangzongwu 你好,这里是高阶组件的写法,类似于高阶函数。
JS中可以把函数作为参数,传入到另一个函数中,比如我们常用的通过map去渲染多个相同组件的时候,这里map就是这种应用(可以延伸学习函数式编程)。
函数式编程
高阶组件与此类似,就是可以传入一个组件,对该组件做一些自己的加工,然后返回加工后的组件。比如这个NetInfoDecorator,就是为传入的组件添加了网络状态的监听,并把网络连接标志位 isConnected作为props,传递到传入的组件去。
NetInfoDecorator
isConnected
props
当然这里应用不一定准确,只是为了练习这种写法,什么时候要用高阶组件,还要看你实际情况。另外推荐《深入React技术栈》,这本书也是比较深入地讲解了这些内容的~
《深入React技术栈》
Sorry, something went wrong.
加油
No branches or pull requests
**const NetInfoDecorator = WrappedComponent => class extends Component {** constructor(props) { super(props) this.state = { isConnected: true, } }
是在函数中返回一个组件吗?把组件的名字作为参数?
The text was updated successfully, but these errors were encountered: