Skip to content
ClapelloNotes by Barath
ReactCh 11 · 6/6
Deep dive4 min readUpdated

React / 63

Updating context and keeping it fast

TL;DR

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:

ThemeContext.jsx
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);
}
ThemeToggle.jsx
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 }}>

<ThemeBadge />waiting

reads: theme

theme: dark
<UserBadge />waiting

reads: user

user: Asha
<Sidebar />waiting

reads: no context

Only components that read a context re-render when its value changes. With one combined context, changing the theme also re-renders UserBadge. Split it, and each change touches only its own readers. Sidebar never re-renders: it reads nothing and is passed in as children.

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:

ThemeContext.jsx
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?

SituationReach for
Data needed one or two levels downProps
Data passing through components that don't use itComposition (children) first
App-wide, rarely changing values: theme, user, languageContext
Big shared state, updated often, many readersRedux 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.