我正在使用React with Redux并有这个问题。
首先,我创建 EditorContainer
引入 store
的组件并呈现:
EditorContainer.jsx:
<Provider store={store}>
<Editor></Editor>
</Provider>
在 EditorContainer.jsx 中分派(dispatch)操作效果很好。
但是我也想在 Editor.jsx 中调度一些操作,所以我这样做了:
编辑器.jsx
class Editor extends React.Component {
componentWillMount() {
store.dispatch(doSomething());
}
render() {
return (
// code
);
}
}
const mapStateToProps = state => ({
somethingPayload: somethingPayload
});
export default connect(mapStateToProps)(Editor);
当然,我在第 3 行收到Uncaught ReferenceError: store is not Defined。
不应该connect
在这里完成工作并包括 store
& dispatch
在我的例子中的编辑器组件中?
最佳答案
您应该使用 this.props.dispatch
而不是 store.dispatch
,因为 dispatch
函数是通过 props 传递的。
关于javascript - React-Redux - 如何从子组件分派(dispatch)操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47497724/