mapDispatchToProps
Explore how mapDispatchToProps works within Redux to dispatch actions from React components. Understand writing action creators, using shorthand notation, and combining with mapStateToProps for streamlined state management.
We'll cover the following...
Dispatching actions via mapDispatchToProps
Let’s look at the second parameter for the connect() function, mapDispatchToProps:
const mapDispatchToProps = (dispatch, ownProps) => {
// ...
};
Or, it can alternatively be written as follows:
const mapDispatchToProps = {
// ...
};
While mapStateToProps grants us access to the store to read data, mapDispatchToProps allows us to change the store’s data with write access. The mapDispatchToProps function’s
signature looks similar to that of mapStateToProps. However, instead of receiving the whole state as a first parameter, we receive the dispatch method of the store that we connect to. The second parameter of mapDispatchToProps form ownProps-the props of the component
itself - that is passed to the component. It is also possible to pass a ...