Reputation: 609
I have a component that doesn't need to have access to the global state, but does need to dispatch an action creator.
It seems there are two alternatives:
mapStateToProps
and passing my action creator via mapDispatchToProps
- from what I can make out, passing the global state like this is not a good idea thoughAre there any other alternatives?
Upvotes: 3
Views: 288
Reputation: 10391
You can just pass null
, instead of mapStateToProps
function.
There are ways to achieve the goal:
1.Inject just dispatch
function and don't listen to store
export default connect()(Component);
then in component
this.props.dispatch(actionCreator());
2.Inject actions creators
and don't listen to store
import * as actionCreators from './actionCreators'
export default connect(null, actionCreators)(Component);
then in component
this.props.actionCreator();
BTW, you should never pass the global state. It kills any performance optimizations because Component will rerender after every action.
Upvotes: 1