consider always passing down dispatch regardless of what mapDispatchToProps does
See original GitHub issueI’m experimenting with composing several higher-order-components, like so:
compose(
connect(mapStateToProps),
authorize(unauthorizedAction),
loadData(loadDataAction)
)(MyComponent);
It would be really nice if I could count on connect-ed components always having dispatch defined. Because if I do this:
compose(
connect(mapStateToProps, { someAction, anotherAction }),
authorize(unauthorizedAction),
loadData(loadDataAction)
)(MyComponent);
…then my dispatch goes away in the connected component, and I have to implement a much more awkward mapDispatchToProps in order to get it back.
It’s worth noting that redux-form also suffers from this problem.
IMO, being able to rely on some consistent props in a connected component would make this kind of third-party composition a lot easier.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:14 (8 by maintainers)
Top Results From Across the Web
What's the use of mapDispatchToProps when we can just use ...
Yes, you can certainly use props.dispatch() directly in a component. However, that component now "knows" that is part of a Redux application ...
Read more >Connect: Dispatching Actions with mapDispatchToProps
Providing a mapDispatchToProps allows you to specify which actions your component might need to dispatch. It lets you provide action dispatching ...
Read more >You Might Not Need mapDispatchToProps : r/reactjs - Reddit
First, if you look at React-Redux issue #1, you can see that the idea of passing an object of action creators and having...
Read more >3 small tips for better Redux performance in a React app
What some people don't know, is that whenever you use the component's props from your mapDispatchToProps (whenever you use the 2nd argument of...
Read more >You Might Not Need The mapDispatchToProps Function
You can avoid having to call action creators with dispatch by passing a second argument to connect, the mapDispatchToProps function. Here's the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Both approaches are extreme. We suggest taking the middle approach.
Try to create presentational components and use them when possible. However when you feel like you’re passing data through components in the middle that don’t use it, it’s a good signal that it’s time to create a container component for that presentational component.
So basically create containers when you feel like some particular component has become hard to reuse because it requires too much unrelated data that parent components don’t care about.
I strongly suggest you to watch the last 10 videos of my course because they deal with this particular question: https://egghead.io/series/getting-started-with-redux
My question is it seems that both in this thread and on S.O. there are two points of view on how to compose components with Redux.
I understand from the S.O. thread that there are tradeoffs and benefits to each of the above approaches but I don’t understand what the ideal way would be. Is it 1 or 2 or a combination of both as suggested in the accepted S.O. answer? Or is it some other model. I’m trying to understand how best to compose components when using Redux.
Your analysis in that S.O. post is very clear, and you seem to have been looking for the right solution but it’s not clear what that is, and more importantly how composition should be done in Redux.