DEV Community

Or Yoffe
Or Yoffe

Posted on

1

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?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay