-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Switch the Context to use the Parent tree instead of the Owner tree #2112
Comments
I'd be a big +1 for this. This would really make it much easier to design complex components in react. |
@sebmarkbage I'm not entirely sure exactly what problem contexts are meant to solve. But in the hypothetical context of reinvented styles (without selectors) I can imagine a benefit in being able to propagate It seems that using a GUID style approach would be preferable for avoiding any and all conflicts, components would instead opt-in. Something like:
But again, I'm not exactly sure what contexts are meant to solve so I could be way way of the mark here. |
If someone who is more familiar with the React internals would care to put together a short summary of what needs to be done here, I'd be glad to give it a shot and make a PR. react-router depends heavily on Things I don't fully understand:
|
I'd love to know more about when/if this might happen and what it will take to get there. I share @1st1's belief that this will make component/library composition better. In my particular case, I'm working on a library that wraps ReactRouter, and I'd like to be able to pass data down from my library, through the routes, and into the app's components. I can't presently do this without either a) changes by ReactRouter to support this directly, or b) the ability to pass a context into an app's already-created |
@sebmarkbage I assume we'll want to support reparenting someday… is there anything we need to keep in mind while doing this? Guessing not but wanted to check. |
Things I learned yesterday while talking to @sebmarkbage about this:
|
cc @JSFB |
+1 for this. So happy this is finally being worked on! We use context indirectly through react-router, and use it directly for analytics (e.g. in which of several "areas" of the screen / on which screen was "Sign Up" button pressed). Another use case is when we want some pages to be "themed" with a specific background color and we'd like all controls, no matter how deep in hierarchy, be able to use this color (can't do this in CSS because we want to calculate whether text needs to be dark or light based on that, etc). |
Start monitoring uses of withContext, related to issue #2112
@jamesknelson is this enabled in RC2 or would it be worth updating and trying the patch there? |
Is there any chance that we might get some documentation on how contexts work? I've seen a vague mention of this feature on the React blog. I've seen it in use in other projects such as React-router and perhaps also Reapp. There is code in those projects that throws warnings and needs migrating. I'd love to contribute but I feel unable to help make the situation better because I'm still in the dark about how contexts are supposed to work. |
@zenlambda maybe this will help https://www.tildedave.com/2014/11/15/introduction-to-contexts-in-react-js.html |
Just as a heads up to anyone using the contextPatch and running into If your child Class is attempting to use an undefined component this happens. In my case I was trying to import "Textarea" rather than "TextArea". I was certain the error was due to how I was mounting routes (in the parent class) and funny enough both children routes I tried had this same Textarea bug, so I spent forever changing the parents until I got this. 💀 |
Any chance this will land to 0.14? |
@1st1 That's the plan. |
Switch to parent-based context. Fixes #2112.
This reverts commit 7d44917. Only doing this temporarily here to make it easier to sync FB
This reverts commit 7d44917.
Conflicts: src/core/__tests__/ReactCompositeComponent-test.js
Does React Native use parent context now as well? Seems like it still uses owner context. |
React Native is still on React 0.13.2. We'll update after the 0.14 final release. |
Cool, glad to know this is on the way! I've also used context to pass down stores, out of a desire to avoid singletons. I've used them in a few other cases as well. |
This behavior has long been fixed (facebook/react#2112) and is causing errors when we have “null” children inside of a Form (a common pattern in more modern versions of React)
I'm pretty convinced at this point that Contexts are more useful in the Parent tree than the Owner tree. It also opens up new use cases for parent->child communication that wasn't possible before.
The text was updated successfully, but these errors were encountered: