Redux and mapDispatchToProps

I have found Redux challenging and am taking it inch by inch. React was the same at first so I am confident the grinding period will be over soon enough.

I wanted to ask how mapDispatchToProps is usually handled. Do most devs leave it out and just wrap ReactRedux.connect before exporting any store-touching component? Or do they define it in the parent and update it as needed?

I’m still working through dispatches so option 1 is what I’m using for now.

Don’t worry, you’ll fully get Redux after some period of time. I was also struggling with it, but now it’s very straightforward to me. About your question, it’s probably better to define your mapDispatchToProps function in order to create clean and readable code. And also, you don’t need to call it like that, it’s only a convention.
So it can look something like this:

const PresentationalComponent = () => {
  // React Component
}

const mapStateToProps = () => {
  return {
    // here you return state properties you want to pass to your Presentational Component
  }
}

const mapDispatchToProps = () => {
  return {
    // here you return some actions you want to pass to your Presentational Component
  }
}

const ContainerComponent = connect(mapStateToProps, mapDispatchToProps)(PresentationalComponent);
1 Like