Skip to content

What about a mapContextToProps? #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gfpacheco opened this issue Jan 12, 2016 · 3 comments
Closed

What about a mapContextToProps? #247

gfpacheco opened this issue Jan 12, 2016 · 3 comments

Comments

@gfpacheco
Copy link

My root component have a property and I want to pass it down to any descendants that care about it. From what I've seen, please tell me if I'm wrong, the right way of doing it is through context. This way no intermediate component should know about it and keep passing it down.

The problem is my container (smart component, or whatever) doesn't have access to context to send that property to my presentational component as a prop (or does it?).

If I'm right again, is there any reason to not exist such map?

@timjacobi
Copy link

context is to be used carefully as it is effectively a global variable. The philosophy of redux is that all your application state lives in the store. Using redux with React the store is actually made available to all components through context by using the <Provider /> component which is part of react-redux.
You could incorporate the property you are referring to into the store and then use mapStateToProps to make the property available to your components.

@xorcus
Copy link

xorcus commented Jan 15, 2016

Why at all you need to separate mapStateToProps, mapDispatchToProps, etc. ?
Why not just have a single connect() argument mapToProps( { state, dispatch, context, ... }) ?
The background of this question is that one may want to access the state in mapDispatchToProps for some reason... e.g. decide which action to dispatch based on the current state, etc.

@gaearon
Copy link
Contributor

gaearon commented Jan 15, 2016

I don't think we want to rely on context in our public API. It's still subject to change. You should create an intermediate component that takes care of reading from context and rendering the child component.

We don't suggest you to use context for changing data because React doesn't support this scenario well at the time (facebook/react#2517).

Why at all you need to separate mapStateToProps, mapDispatchToProps, etc. ?
Why not just have a single connect() argument mapToProps( { state, dispatch, context, ... }) ?

Check out #1 for a big discussion on this. ;-)
Basically it's a perf nightmare because you'd have to rebind action creators every time the state changes.

This means a ton of unnecessary and expensive allocations, and changing function identities that kill shouldComponentUpdate() optimizations down the tree.

The background of this question is that one may want to access the state in mapDispatchToProps for some reason... e.g. decide which action to dispatch based on the current state, etc.

You can already use mergeProps() (third argument to connect()) to do something with the results of mapStateToProps() and mapDispatchToProps(). If you really want to, you can put that logic there. See also #237.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants