问题描述
我学习了 React Redux 并阅读了 here 关于使用 mapdispatchToProps
和 bindActionCreators
单独使用 bindActionCreators
时效果很好,如下所示:
export default connect(null,(dispatch) =>
bindActionCreators(actionCreators,dispatch)
)(FormContainer);
但我有更多的动作并尝试了这个:
function mapdispatchToProps(dispatch) {
return {
bindActionCreators(actionCreators,dispatch),clearPosts: () => dispatch(clearPosts()),}
}
这会产生如下错误:
。我如何将它们结合起来,我阅读了docs,但我对此一无所知,请提供建议
解决方法
现在建议使用“地图对象”表示法如果您真的想使用connect
。
const mapDispatchToProps = {
...actionCreators,clearPosts
}
此外,如果您可以使用函数组件,建议是 to use redux hooks instead of connect,因为它们通常更易于使用。
(如果您正在学习向您展示 connect
的教程,它可能已经过时,请改为查看 official redux tutorials)
试试下面的代码
function mapDispatchToProps(dispatch) {
return {
todoActions: bindActionCreators(actionCreators,dispatch),clearPosts: () => dispatch(clearPosts()),}
}