Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does useContext work without Context.Provider?

Can you explain me why useContext() works for me if I don't wrap my components with Context.Provider? I just exported result of "createContext(someValues)" with predefined values and called useContext(exportedContext) in component and it works. Every tutorial tells me to wrap components. Has something changed in React?

like image 667
JavaFox Avatar asked Feb 22 '26 07:02

JavaFox


1 Answers

All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes. In other words, If you don't wrap your components with Context.Provider they won't get re-rendered when the someValues in createContext(someValues) changes. You will get the initial value that you set only in the first render.

Demo here

like image 72
Othmani Ali Avatar answered Feb 23 '26 19:02

Othmani Ali