this.props.dispatch() 未定义使用重组生命周期({})
this.props.dispatch() undefined using recompose lifecycle({})
所以我正在尝试使用 Recompose 将我的生命周期方法移动到 HOC 装饰器中。像这样...
export const fetchOptions= lifecycle({
componentDidMount() {
this.props.dispatch(change('mainForm', 'orderHeader.proxies', this.props.currentSalesRepId));
},
componentDidUpdate(prevProps) {
if (this.props.total != prevProps.total){
this.props.dispatch(change('mainForm', 'totalPurchase', this.props.total));
}
}
});
然后我尝试将它添加到呈现我所有标记的表单组件中。像这样。
export default compose(
connect(mapState, mapDispatch),
fetchOptions,
)(MainReduxForm)
我一直收到 this.props.dispatch 不是函数的错误...有什么想法吗?我故意保持这个简短以避免文字墙。如果您需要更多信息,请告诉我!
这里的问题是,如果您不提供 mapDispatchToProps
函数或在您的情况下,dispatch
方法只会自动添加到您的道具中。 mapDispatch
所以你有两个选择,从你的连接呼叫中删除 mapDispatch
。
export default compose(
connect(mapState),
fetchOptions,
)(MainReduxForm)
或者如果你在你的 mapDispatch
中做一些事情,只需将 dispatch 添加到你的 return 对象
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
...
other dispatch functions here
...
}
}
}
所以我正在尝试使用 Recompose 将我的生命周期方法移动到 HOC 装饰器中。像这样...
export const fetchOptions= lifecycle({
componentDidMount() {
this.props.dispatch(change('mainForm', 'orderHeader.proxies', this.props.currentSalesRepId));
},
componentDidUpdate(prevProps) {
if (this.props.total != prevProps.total){
this.props.dispatch(change('mainForm', 'totalPurchase', this.props.total));
}
}
});
然后我尝试将它添加到呈现我所有标记的表单组件中。像这样。
export default compose(
connect(mapState, mapDispatch),
fetchOptions,
)(MainReduxForm)
我一直收到 this.props.dispatch 不是函数的错误...有什么想法吗?我故意保持这个简短以避免文字墙。如果您需要更多信息,请告诉我!
这里的问题是,如果您不提供 mapDispatchToProps
函数或在您的情况下,dispatch
方法只会自动添加到您的道具中。 mapDispatch
所以你有两个选择,从你的连接呼叫中删除 mapDispatch
。
export default compose(
connect(mapState),
fetchOptions,
)(MainReduxForm)
或者如果你在你的 mapDispatch
中做一些事情,只需将 dispatch 添加到你的 return 对象
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
...
other dispatch functions here
...
}
}
}