【发布时间】:2017-07-10 12:52:06
【问题描述】:
这里有一个应用根组件通过克隆将一个handleHeaderTitle 函数传递给它的所有子组件。 在子组件内部,该函数在 componentWillMount() 中调用 因此根组件会根据路由更新标题文本。
根:
{
this.props.children &&
React.cloneElement(this.props.children, {
handleHeaderTitle: this.handleHeaderTitle
})
}
handleHeaderTitle(title) {
this.setState({ headerTitle: title });
}
孩子:
componentWillMount() {
this.props.handleHeaderTitle("Profile");
}
Profile.propTypes = { handleHeaderTitle: React.PropTypes.func };
这是添加 redux 的正确用例,因为这里的状态并不是真正的本地状态,因为它是从其父组件上的子组件设置的?
我想我需要创建像 SET_PROFILE_HEADER 和内部 reducer 这样的操作,将它们结合起来。但仍在弄清楚如何通知容器根组件子组件已更改标题标题
【问题讨论】:
标签: reactjs redux react-redux