React / 62
Context: share data without passing props
Create a context, wrap part of the tree in a provider with a value, and read it from any component below with useContext or use.
The idea
1 · Create it
Call createContext once, outside any component, usually in its own file so others can import it:
import { createContext } from "react";
export const UserContext = createContext(null);The argument is the default value, used only when there's no provider above (more on that below).
2 · Provide a value
Wrap the part of the tree that needs the data. Everything inside, at any depth, can read it:
import { useState } from "react";
import { UserContext } from "./UserContext";
export default function App() {
const [user] = useState({ name: "Asha" });
return (
<UserContext value={user}>
<Layout />
</UserContext>
);
}3 · Read it anywhere below
import { useContext } from "react";
import { UserContext } from "./UserContext";
export default function UserMenu() {
const user = useContext(UserContext);
return <p>Hi, {user.name}</p>;
}No user prop on Layout, Header or anything in between. React 19 also has use(UserContext), which does the same thing, except that use can be called inside an if or a loop:
import { use } from "react";
function Greeting({ show }) {
if (!show) return null;
const user = use(UserContext); // allowed: use() can be conditional
return <p>Hi, {user.name}</p>;
}Nearest provider wins, and the default
Flip the provider
Try itconst ThemeContext = createContext("light") // the default
<ThemeContext value="dark">
<Header />
theme = "dark"
inside the outer provider
<MenuCard />
theme = "dark"
inside the outer provider
<ThemeContext value="light">
<Footer />
theme = "light"
nearest provider wins
no provider above
<Toast />
theme = "light"
falls back to the default
- A component reads the nearest provider above it. An inner provider overrides the outer one, just for its part of the tree, like CSS inheritance.
- With no provider at all above it, a component gets the default value from
createContext. - A provider without a
valueprop givesundefined, not the default.
A custom hook makes it safer
Wrap useContext in a small hook so every component reads it the same way, and a missing provider fails loudly instead of crashing on undefined.name:
import { createContext, useContext } from "react";
export const UserContext = createContext(undefined);
export function useUser() {
const user = useContext(UserContext);
if (user === undefined) {
throw new Error("useUser must be used inside <UserContext value={…}>");
}
return user;
}Now components just write const user = useUser();.
In class components
Class components can't call hooks. They read one context with static contextType, or any context with the older Consumer render function:
import { Component } from "react";
import { UserContext } from "./UserContext";
class UserMenu extends Component {
static contextType = UserContext;
render() {
return <p>Hi, {this.context.name}</p>;
}
}<UserContext.Consumer>
{(user) => <p>Hi, {user.name}</p>}
</UserContext.Consumer>You'll see both in older code. New code uses useContext or use.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
