javascript - 将多个操作传递给 bindActionCreators

标签 javascript reactjs redux

我似乎找不到这个问题的具体答案。

如果我有一个连接的组件,并且我想从不同的 reducer 传递多个 Action :

import { actions as commonActions } from 'some/action/file';
import { actions as userActions } from 'another/action/file';

我可以在 mapDispatchToProps 调用中像这样使用 bindActionCreators...吗?

const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators((commonActions, userActions), dispatch)
});

或者我必须像这样传递它们

actions: bindActionCreators({ ...commonActions, ...userActions }, dispatch);

它们像后者一样在我使用的整个应用程序中传递,但我正在清理和更新应用程序,并希望尽量减少任何可能的地方。前者是可以接受的,还是必须作为解构对象传入?

谢谢!

最佳答案

你会想要使用 Object.assign:

return {
     actions: bindActionCreators(Object.assign({}, commonActions, userActions), dispatch) 
};

您可以直接在商店上调用操作,以便您可以删除 bindActionCreators: this.props.store.dispatch(sendMessage())

This article Mark Erikson 详细介绍了 bindActionCreators 的替代方案,因为他建议不要使用它。

关于javascript - 将多个操作传递给 bindActionCreators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56024942/

相关文章:

javascript - 通过 Id 比较两个数组的元素,并从一个数组中删除另一个数组中未出现的元素

javascript - 在 shadow-root 中注入(inject) CSS 样式而不是 head 标签 | Vue.js 和 Webpack

reactjs - 在事件处理程序中调用 Hook (useMutation) 错误 : "Hooks can only be called inside of the body of a function component"

reactjs - 需要有关 ReactNative、Redux 和 Navigator 的一些概念的帮助

reactjs - 我应该在 redux 存储中存储高吞吐量、结构化传感器数据吗?

reactjs - 如何在 saga 异步请求后设置状态

javascript - 正则表达式如果子字符串存在则匹配字符串的另一部分

javascript - 插入失败 : Method in meteor 1. 3 并使用react

reactjs - React 路由器重新渲染整个应用程序并重置 redux 状态树?

javascript - 在 Ajax 请求中使用 $.when