4 ms·
As someone who stopped doing React development before Context became a "thing", is it possible to ELI5 in a couple sentences why it is such an important concept
by gramstrong 7y ago
As someone who stopped doing React development before Context became a "thing", is it possible to ELI5 in a couple sentences why it is such an important concept?
- revskill 7y agoIt's important, because you don't need to care about props vs state, instead, just consume the context deep in your component tree. 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.
- eropple 7y agoIMO, context does a lot to remove the value of MobX from React applications. Not necessarily Redux; Redux gives you some additional stuff, particularly around state tracking and rewinding. But I haven't seen a MobX project where a judiciously used context doesn't solve the same problem within the React-specific ecosystem.
- deleted 7y ago[deleted]
- hombre_fatal 7y agoThe value of mobx is that you implicitly link each component to its data dependencies by dereferencing arbitrary paths into your storage objects in the render() function so that the component rerenders only when data at those paths change. Does the Context achieve this as well? From a quick search, the Context seems to force rerender of any consumer on any change.
- revskill 7y agoYou can use library like reselect to optimize. The job of context is simple: implicitly passing down value to deep component.
- thereare5lights 7y agoThis is starting to sound like the scope from Angular 1.
- pbedat 7y agoYep. That's why context should be used very very rarely or never. When I teach react, I try to make a long detour around them. I would not consider it being the most important concept. Its only a side effect model.
- munchbunny 7y agoContexts in React are like raw pointers in C++. There are reasons to use them, but every time you do, it should raise eyebrows, because they are finicky, complicated to reason about, and thus full of pitfalls.
- draw_down 7y agoReact Context is essentially scoped global props (the scope is some subtree of the overall component tree; the props are global to that subtree, any component in the subtree can elect to consume them). Why it's an important concept is that it keeps you from needing to pass props down the tree from each component to its children. This is important because often times those intermediate components don't have any concern with the stuff being passed down, so they shouldn't be concerned with data that doesn't matter to them. 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.