我似乎找不到这个问题的具体答案。
如果我有一个连接的组件,并且我想从不同的 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/