当一个组件只需要派发一个 action creator 时应该使用什么模式?

What pattern to use when a component only needs to dispatch an action creator?

我有一个组件不需要访问全局状态,但需要分派动作创建者。

看来有两种选择:

还有其他选择吗?

您可以只传递 null,而不是 mapStateToProps 函数。

实现目标的方法有:

1.只注入dispatch函数,不听store

export default connect()(Component);

然后在组件中

this.props.dispatch(actionCreator());

2.注入actions creators不听store

import * as actionCreators from './actionCreators'

export default connect(null, actionCreators)(Component);

然后在组件中

this.props.actionCreator();

顺便说一句,你永远不应该传递全局状态。它会扼杀任何性能优化,因为组件将在每次操作后重新呈现。