高阶组件
Higher Order Components
使用 HOC 解决横切关系点问题
componentDidMount() {
// 订阅更改
DataSource.addChangeListener(this.handleChange);
}
componentWillUnmount() {
// 清除订阅
DataSource.removeChangeLister(this.handleChange);
}
handleChange() {
// 当数据源更新时,更新组件状态
this.setState({
comments: DataSource.getComments()
})
}
render() {
return (
<div>
{this.state.comments.map((comment) => (
<Comment comment={comment} key={comment.id} />
))}
</div>
)
}不要改变原始组件 使用组合
约定:将不相关的 props 传递给被包裹的组件
约定:包装显示名称以便轻松调试
Refs 不会被传递
最后更新于