DEV Community

Or Yoffe
Or Yoffe

Posted on

why does react context need the provider?

Hi everyone, i wondered why using context provider is necessary and couldn't find the answer (besides that it's the react api) and therefore posted a stack overflow question with more details. I would love your opinion

https://stackoverflow.com/questions/61693937/why-does-react-context-needs-a-provider

Top comments (3)

Collapse
 
nialljoemaher profile image
Niall Maher

It doesn't "need" the provider. If you remove the provider it will fall back to whatever the default was set in the initial createContext.

Without a provider, you can't make it dynamic so it would only have the static values you set up as the defaults. Basically, the provider makes context a lot more flexible.

Collapse
 
oryoffe profile image
Or Yoffe

Yeah i think that complicates the context state management.

You could expose a setState function of the context (just like Consumer and Provider) and allow your app to change the context state from anywhere you wish and not just from the provider which is usually at the root of the tree and causes a lot of unnecessary rendering

Collapse
 
oryoffe profile image
Or Yoffe

Nice, though i don't understand how it's related to the topic?