J_P
J_P

Reputation: 609

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

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:

Are there any other alternatives?

Upvotes: 3

Views: 288

Answers (1)

Kokovin Vladislav
Kokovin Vladislav

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

Related Questions