如何解决多重动作时关于 mapDispatchToProps 和 bindActionCreators 的问题
我学习了 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()),}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。