mapDispatchToProps

While mapStateToProps allows a component to subscribe to a store's state changes, mapDispatchToProps allows a component to dispatch actions to the store.

It is called with a reference to the store’s dispatch method, and should return an object where each key maps to a function that calls the dispatch method.

For instance, our mapDispatchToProps function may look like this:

function mapDispatchToProps (dispatch) {   return {     handleInputChange: (name, event) => {       const value = event.target.value;       const action = {         type: 'LoginForm:update',         field: name,         value       }       dispatch(action);     }   }; };

The handleInputChange key will get merged into the component's props, and be available in the component’s methods as this.props.handleInputChange ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.