DEV Community

Discussion on: Why I'll definitely use mapDispatchToProps in Redux

Collapse
 
markerikson profile image
Mark Erikson

Out of curiosity, was there a particular reason why you hadn't been using mapDispatch prior to this?

Also, note that we specifically recommend that you use the "object shorthand" form of mapDispatch rather than the function form:

const mapDispatch = {doSomething : somethingDispatchable};

On the other hand, note that with our new React-Redux hooks API, there's just a useDispatch() hook that gives you access to dispatch.

I talked about some of the differences between connect and the hooks API in my post Thoughts on React Hooks, Redux, and Separation of Concerns and my ReactBoston 2019 talk on Hooks, HOCs, and Tradeoffs.

Collapse
 
keraito profile image
Chak Shun Yu

Not in particular. I never came across the docs for mapDispatch, so I didn't know of its existence. I also didn't see it before in projects that I either worked on or contributed to, so that didn't help either.

I consciously decided to illustrate the function form, because I feel like it better describes what mapDispatch is and what its purpose is. But I definitely like the object shorthand!

While looking into mapDispatch after the first time I learned about it, I also came across useDispatch. I haven't read the links that you mentioned yet (but definitely will do!), but I personally rather stick with the HOC construction. It keeps class and function components interchangeable (at my job we use class components) and I feel like using useDispatch is very similar to just connecting a component and calling the dispatch function, which kind of equals not using mapDispatch. I prefer the explicitness the HOC approach provides in tests and how easy it becomes to validate them.