首页 > 解决方案 > 如何在 React Redux 中将多个调度操作映射到单个道具

问题描述

按下提交按钮时,我需要调度两个动作。

const mapDispatchToProps = dispatch => ({
    onSubmitPressed: countryCode => dispatch(createCountry(countryCode)),
    onSubmitPressed: countryCode => dispatch(loadCountry(countryCode)),
}); 

上面的代码有效,但给出了警告

重复键“onSubmitPressed”

如何修改代码以将两个调度操作映射到单个道具而不复制密钥。

标签: reactjsreduxreact-redux

解决方案


const mapDispatchToProps = dispatch => ({
    onSubmitPressed: countryCode => {
      dispatch(createCountry(countryCode));
      dispatch(loadCountry(countryCode));
    }
}); 

推荐阅读