useContext
useContext is a React Hook that lets you read and subscribe to context from your component.
const value = useContext(SomeContext)
Reference
useContext(SomeContext)
Call useContext at the top level of your component to read and subscribe to context.
import { useContext } from 'react';
function MyComponent() {
const theme = useContext(ThemeContext);
// ...
Parameters
SomeContext: The context that you've previously created withcreateContext. The context itself does not hold the information, it only represents the kind of information you can provide or read from components.
Returns
useContext returns the context value for the calling component. It is determined as the value passed to the closest SomeContext above the calling component in the tree. If there is no such provider, then the returned value will be the defaultValue you have passed to createContext for that context. The returned value is always up-to-date. React automatically re-renders components that read some context if it changes.
Caveats
useContext()call in a component is not affected by providers returned from the same component. The corresponding<Context>needs to be above the component doing theuseContext()call.- React automatically re-renders all the children that use a particular context starting from the provider that receives a different
value. The previous and the next values are compared with theObject.iscomparison. Skipping re-renders withmemodoes not prevent the children receiving fresh context values. - If your build system produces duplicates modules in the output (which can happen with symlinks), this can break context. Passing something via context only works if
SomeContextthat you use to provide context andSomeContextthat you use to read it are exactly the same object, as determined by a===comparison.
Usage
Passing data deeply into the tree
Call useContext at the top level of your component to read and subscribe to context.
```js [[2, 4, "theme"], [1, 4, "ThemeContext"]] import { useContext } from 'react';
function Button() { const theme = useContext(ThemeContext); // ...
`useContext` returns the <CodeStep step={2}>context value</CodeStep> for the <CodeStep step={1}>context</CodeStep> you passed. To determine the context value, React searches the component tree and finds **the closest context provider above** for that particular context.
To pass context to a `Button`, wrap it or one of its parent components into the corresponding context provider:
```js [[1, 3, "ThemeContext"], [2, 3, "\\"dark\\""], [1, 5, "ThemeContext"]]
function MyPage() {
return (
<ThemeContext value="dark">
<Form />
</ThemeContext>
);
}
function Form() {
// ... renders buttons inside ...
}
It doesn't matter how many layers of components there are between the provider and the Button. When a Button anywhere inside of Form calls useContext(ThemeContext), it will receive "dark" as the value.
[!WARNING]
useContext()always looks for the closest provider above the component that calls it. It searches upwards and does not consider providers in the component from which you're callinguseContext().
import { createContext, useContext } from 'react';
const ThemeContext = createContext(null);
export default function MyApp() {
return (
<ThemeContext value="dark">
<Form />
</ThemeContext>
)
}
function Form() {
return (
<Panel title="Welcome">
<Button>Sign up</Button>
<Button>Log in</Button>
</Panel>
);
}
function Panel({ title, children }) {
const theme = useContext(ThemeContext);
const className = 'panel-' + theme;
return (
<section className={className}>
<h1>{title}</h1>
{children}
</section>
)
}
function Button({ children }) {
const theme = useContext(ThemeContext);
const className = 'button-' + theme;
return (
<button className={className}>
{children}
</button>
);
}
.panel-light,
.panel-dark {
border: 1px solid black;
border-radius: 4px;
padding: 20px;
}
.panel-light {
color: #222;
background: #fff;
}
.panel-dark {
color: #fff;
background: rgb(23, 32, 42);
}
.button-light,
.button-dark {
border: 1px solid #777;
padding: 5px;
margin-right: 10px;
margin-top: 10px;
}
.button-dark {
background: #222;
color: #fff;
}
.button-light {
background: #fff;
color: #222;
}
Updating data passed via context
Often, you'll want the context to change over time. To update context, combine it with state. Declare a state variable in the parent component, and pass the current state down as the <CodeStep step={2}>context value</CodeStep> to the provider.
```js {2} [[1, 4, "ThemeContext"], [2, 4, "theme"], [1, 11, "ThemeContext"]]
function MyPage() {
const [theme, setTheme] = useState('dark');
return (
