Context API
The Context API is React's built-in way to share data across many components without passing props through every level. You create a Context, provide a value at the top, and read it anywhere below with useContext.
React
Context solves prop drilling — the chore of threading the same prop through layers of components that don't even use it. You create a Context with createContext, wrap a part of your tree in its Provider with a value, and any descendant can read that value.
Context is best for data that many components need, such as the current theme, the logged-in user, or the chosen language. It is not a full state-management system, but it pairs well with state Hooks for app-wide data.
Examples
JSX — Profile reads the user object from context and renders 'Signed in as Ana' without receiving any props.
import { createContext, useContext } from "react";
const UserContext = createContext(null);
function Profile() {
const user = useContext(UserContext);
return <p>Signed in as {user.name}</p>;
}
function App() {
return (
<UserContext.Provider value={{ name: "Ana" }}>
<Profile />
</UserContext.Provider>
);
}