by revskill · 7y ago · 0 comments
Context is the most important concept in React.
Actually, a library just need users to provide the right types of Context to be flexible.
I'm tired of the "lock in" API, in which, it doesn't allow me to just get my context data into its arguments API.
In this sense, you can consider Context as the local version of Redux.
One use case, is, suppose you have two independent contexts at two branches, now, if you want to share those data to 3rd branch, you just create new context with value from the first two contexts.
Does the Context achieve this as well? From a quick search, the Context seems to force rerender of any consumer on any change.
Another way of passing around ~global stuff is Redux, which many people find cumbersome and difficult to understand. And by "many people" I mean me.
Context is fully opt in. A Context provider upstream will have no observable effect on your downstream components unless you have a component that explicitly chooses to consume _that particular context_.
I think you're thinking of the old Context API where everything merged onto the same shared context object and read from it implicitly. _That_ was a nightmare, hence why it was never recommended for general use, but the new API doesn't suffer from the same issues.
Then in all components props, you'll have your own context value at every level deep in the tree.