ReactIntermediate#hooks#context

What is useContext and when do you use it?

useContext reads a value from the nearest matching Provider, avoiding prop drilling for global-ish data such as theme, locale or the current user. Every consumer re-renders when the context value changes.

Example
const ThemeCtx = createContext('light');
<ThemeCtx.Provider value="dark"><App /></ThemeCtx.Provider>
const theme = useContext(ThemeCtx);

Related Questions

1
ReactAdvanced#hooks#state

When should you use useReducer instead of useState?

Open
2
ReactIntermediate#hooks

How do you create a custom hook?

Open
3
ReactIntermediate#effects#data

How do you fetch data in a React component?

Open