因此,经过几个小时的研究,我得到的问题是,为什么我的组件没有得到this.props.dispatch
。上一个问题:Pass parameters to mapDispatchToProps()。
所以我找到了问题的根源,但不知道如何解决它。我有一个由递归函数创建的嵌套组件。例如:
//App.js render
<Provider>
</ExampleComponent counter = {6}>
</Provider>
//ExampleComponent.js
createChild = () => {
const childs = [];
for (let i = 0; i < this.props.counter; i++){
childs.push(</ExampleComponent counter = {this.props.counter - 1}>)
}
return childs
}
render(){
return this.createChild()
}
export default connect()(ExampleComponent)
&#13;
这里的问题是,只有Provider
的直接组件接收dispatch
作为属性,即使所有组件都相同,并且以理想的方式,它们应该连接。这是一幅图片,了解如何更好地理解:
那么什么是最好的解决方案?我如何连接所有组件? (只有我个人的想法是手动添加调度作为组件的支柱,每个解决方案都很好解决问题)(我需要告诉我使用react-beautiful-dnd也使用redux,所以它可能会冲突我的商店。我不知道,可能是。)
答案 0 :(得分:1)
connect接受2个参数,第一个用于状态,第二个用于发送。
您收到此错误的原因是您忽略了道具。
const mapStateToProps = (state, ownProps) => {
// state has access to your store
// ownProps has access to the props you are passing.
return {
...ownProps,
};
};
const mapDispatchToProps = (dispatch) => {
return {
dispatch: (action) => dispatch(action),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(ExampleComponent);
您还可以在此处导入您的操作,并使用redux中的绑定操作创建器创建调度操作的函数,但我认为不需要。