【发布时间】:2017-09-10 20:15:53
【问题描述】:
我在我的应用开发中使用 React + Electron + Redux。在另一种情况下,我能够从子组件更新父状态,但现在我不能这样做,状态只会更新到子组件。
我知道reducer action是用正确的值调用的,但是父组件被错误的(前一个)重新渲染,只有子组件的子树是以正确的价值呈现。
我的方法:
我正在父组件容器中创建一个函数(动作处理程序):
class CreateExerciseCanvas extends React.Component {
focusOnSection (section) { /* this is the function that i'm refering to */
store.dispatch(actions.focusOnSection(section))
}
render() {
return (
<CreateExerciseCanvas
focusOnSection={ this.focusOnSection }
/>
)
}
}
const mapStateToProps = function (store) {
return {
focusOnSection: store.exercise.focusOnSection
}
}
export default connect(mapStateToProps)(CreateExerciseCanvasContainer)
这个函数作为 prop 传递给子容器:
<Index focusOnSection={ this.props.focusOnSection }/>
最后,该方法在子视图中用作onClick 处理程序。 这不是用 redux + react 更新父级的正确方法吗?
【问题讨论】: