Application-wide configuration
Share a theme, locale, or other ambient configuration with deeply nested components.
React 19+ · Type Alias
createContext defines a context object for passing information through a component tree without explicitly forwarding it as props. Its default value is used only when no matching provider is found above the reader.
In React, a context object represents a kind of information that components can provide or read. Create it outside components with a default value, provide a value from an ancestor using the context object as a provider in React 19, and read the nearest provided value with useContext. The default is used only when no matching provider exists above the reader.
createContext(defaultValue)| Name | Type | Description |
|---|---|---|
defaultValue | any | The fallback value used when a component reads the context without a matching provider above it. It is static and does not change. |
A context object representing the information that components can provide or read.
import { createContext, useContext } from 'react';
type Theme = 'light' | 'dark';
const ThemeContext = createContext<Theme>('light');
function Toolbar() {
const theme = useContext(ThemeContext);
return <button className={theme}>Save</button>;
}
export default function App() {
return (
<ThemeContext value="dark">
<Toolbar />
</ThemeContext>
);
}Creates a context with a fallback value and provides a theme to a deeply nested component.
Share a theme, locale, or other ambient configuration with deeply nested components.
Expose state and dispatch functions to a subtree without prop drilling.
Define a controlled fallback for components rendered outside their intended provider.
useContextuseContext Provider