react-redux-tutorial icon indicating copy to clipboard operation
react-redux-tutorial copied to clipboard

Replace mapDispatchToProps object with a function

Open ndkv opened this issue 6 years ago • 0 comments

First of all, thank you very very much for this great tutorial. It is much, much more clear than the redux and react-redux tutorials/documentations combined!

I have a small suggestion: replace { getData } on line 33 of Posts.js with a mapDispatchToProps function that extends the mapDispatchToProps function used up to this point in the tutorial as follows

function mapDispatchToProps(dispatch) {
  return {
    addArticle: article => dispatch(addArticle(article))
    getData: () => dispatch(getData())
  };
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Post);

I'm suggesting this change since it took me a while to figure out why this.props.getData() on line 11 works without a call to dispatch. It is only after I looked at the connect docs that I learned that passing an object instead of a function to connect binds dispatch to each action creator.

Don' get me wrong: it's useful to know about the Object Shorthand Form. It's just that it's a bit much when learning the basics. :)

ndkv avatar Nov 28 '19 09:11 ndkv