- Home
- /
- Tutorials
- /
- React Tutorial
- /
- useContext Explained
Context
useContext Explained
Context is for values that many components at different depths need - a theme, the current user, a language. It is not a state manager.
The problem it solves
Passing a value through four components that do not use it is called prop drilling. Every layer has to declare and forward a prop it does not care about, and adding a fifth means touching all of them.
The three pieces
createContext(defaultValue)makes the channel.<Context.Provider value={...}>supplies a value to everything inside it.useContext(Context)reads the nearest provider's value.
Skipping the middle layers
jsx
