Capítulo 50 de 116
useContext(SomeContext) reads and subscribes to the value of the nearest enclosing provider for that Context above the calling component — the read half of the Context mechanism introduced conceptually in Ch 32.
const value = useContext(SomeContext) where SomeContext was created with createContext(defaultValue).<SomeContext value={...}> provider — if there is none, it falls back to the defaultValue passed to createContext.value, every descendant calling useContext(SomeContext) re-renders too, regardless of how many non-consuming components sit in between.{ ...several fields } recreated fresh each render.useContext always subscribes — there's no way to read a Context value without also opting into re-rendering when it changes; if only occasional/on-demand reads are needed, that typically points toward passing a getter function through the Context value instead, or restructuring state.const ThemeContext = createContext('light');
function Button() {
const theme = useContext(ThemeContext); // nearest provider's value, or 'light'
return <button className={theme}>Save</button>;
}
Button for theme, yet it resolves correctly based on tree position.useContext always subscribes to future changes of that Context — there's no non-reactive "peek" variant.useMemo) if it's recreated on every provider render, or every consumer re-renders needlessly on unrelated provider re-renders.useContext combined with useReducer for shared mutable state.