React / 63
Updating context and keeping it fast
Put state and its setter in the context value so children can update it, then control re-renders by memoising the value and splitting contexts.
Updating context from a child
Context doesn't store anything itself; it just passes a value down. To make it changeable, keep the value in state and pass the setter along with it:
import { createContext, useContext, useState } from "react";
const ThemeContext = createContext(null);
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
return <ThemeContext value={{ theme, setTheme }}>{children}</ThemeContext>;
}
export function useTheme() {
return useContext(ThemeContext);
}import { useTheme } from "./ThemeContext";
export function ThemeToggle() {
const { theme, setTheme } = useTheme();
return (
<button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
Theme: {theme}
</button>
);
}Wrap the app once, <ThemeProvider><App /></ThemeProvider>, and ThemeToggle works from any depth. This provider component pattern (state + context + a hook in one file) is how most real apps organise context.
What re-renders when the value changes
When a provider gets a new value, React re-renders every component below it that reads that context. It decides "new" with Object.is, the same check as === for objects: a different object counts as changed, even with identical contents.
Who re-renders?
Try it<AppContext value={{ theme, user }}>
reads: theme
reads: user
reads: no context
Notice Sidebar never re-renders. The provider component (like ThemeProvider above) receives the rest of the app as children. Those elements were created by its parent, so they stay the same when the provider's own state changes. Only the readers update.
Fix 1: memoise the value
value={{ theme, setTheme }} creates a new object every time ThemeProvider renders, even if theme didn't change. Keep the same object until something inside actually changes:
import { createContext, useMemo, useState } from "react";
const ThemeContext = createContext(null);
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
const value = useMemo(() => ({ theme, setTheme }), [theme]);
return <ThemeContext value={value}>{children}</ThemeContext>;
}setTheme from useState never changes, so it doesn't need to be in the dependency list. Functions you write yourself should be wrapped in useCallback before going into the value.
Fix 2: split contexts
Memoising doesn't help when the value really changes. If one context holds { theme, user }, a theme change re-renders every user reader too (try it in the demo above). Give unrelated data separate contexts:
<ThemeContext value={theme}>
<UserContext value={user}>
<App />
</UserContext>
</ThemeContext>A common variant: put the value and the setter in two contexts. Components that only change things (a toggle button) read the setter context, which never changes, so they never re-render because of it.
Context, props, or a state library?
| Situation | Reach for |
|---|---|
| Data needed one or two levels down | Props |
| Data passing through components that don't use it | Composition (children) first |
| App-wide, rarely changing values: theme, user, language | Context |
| Big shared state, updated often, many readers | Redux Toolkit, Zustand and similar |
| Data from a server (lists, details) | A data-fetching library like TanStack Query, or your framework |
memo doesn't block context. Wrapping a component in memo stops re-renders caused by its parent, but if the component reads a context that changed, it re-renders anyway.
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.
