你一直以来都错误地使用 mapDispatchToProps
执行以下操作:
const mapDispatchToProps = {
decrement: () => ({ type: "DECREMENT" }),
increment: () => ({ type: "INCREMENT" })
};
而不是这样:
const mapDispatchToProps = dispatch => {
return {
decrement: () => dispatch({ type: "DECREMENT" }),
increment: () => dispatch({ type: "INCREMENT" })
};
};
不同之处在于,我们依靠 react-redux 将调度注入到 mapDispatchToProps 对象中的每个函数值上,而不是依靠 react-redux 将调度注入到 mapDispatchToProps 函数中。
如果您没有有效地测试您的代码,那么这是一种增加代码覆盖率的简单方法,但它也可以防止出现错误,因为您不必担心转发额外的参数。
有关 github 上的工作示例,请参阅此 repo:https://github.com/mcrowder65/map-dispatch-to-props
文章来源:https://dev.to/mcrowder65/you-ve-been-doing-mapdispatchtoprops-wrong-this-entire-time-582m