Sub topic 8
How context updates cause re-renders
This chapter assumes you know JSX, props, useState, and the basics of how React re-renders a component. You should also have seen createContext, a Provider, and useContext. Here you will learn exactly which components re-render when a context value changes, and why.
The rule
When a Provider receives a new value, React compares it with the previous value using Object.is. If they differ, every component that called useContext for that context re-renders. This happens even if an intermediate component between the Provider and the consumer is memoized with React.memo.
If the value is the same, consumers do not re-render because of the context.
Two details matter:
- Only consumers of that specific context are affected. Components that never call
useContext(ThemeContext)are not re-rendered by it. - A consumer re-renders when the value changes, no matter which part of the value it reads. Context has no way to subscribe to one field of an object.
Two separate causes of re-rendering
A component can re-render for two unrelated reasons. Keep them apart when you debug.
- Parent re-render. When a component re-renders, React re-renders its children by default. This is true whether or not they use context.
- Context change. A consumer re-renders because the value it reads changed.
The first cause is why a Provider's own state change often re-renders the whole subtree. The second is why a memoized component can still update.
Seeing it in code
In this example, Toolbar is memoized and receives no props. It logs each render. ThemedButton reads the context.
import { createContext, memo, useContext, useState } from 'react';
const ThemeContext = createContext('light');
const Toolbar = memo(function Toolbar() {
console.log('Toolbar rendered');
return <ThemedButton />;
});
function ThemedButton() {
const theme = useContext(ThemeContext);
console.log('ThemedButton rendered with', theme);
return <button className={`btn-${theme}`}>Save</button>;
}
export default function App() {
const [theme, setTheme] = useState('light');
return (
<ThemeContext.Provider value={theme}>
<Toolbar />
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle theme
</button>
</ThemeContext.Provider>
);
}Click the toggle button. The console shows only ThemedButton rendered with dark. Toolbar is skipped because its props did not change, but React still reaches ThemedButton because it reads the changed context.
Remove memo and click again. Both Toolbar and ThemedButton log, because App re-rendered and Toolbar is its child.
The common trap: a new object every render
Object.is compares objects by reference. A value written inline as an object is a new object on every render of the Provider's parent.
function Provider({ children }) {
const [user, setUser] = useState(null);
// A new object is created on every render of Provider.
return (
<AuthContext.Provider value={{ user, setUser }}>
{children}
</AuthContext.Provider>
);
}Whenever Provider re-renders for any reason, value is a new object. Every consumer then re-renders, even if user did not change. The next chapter shows how to fix this with useMemo.
Primitive values such as strings and numbers do not have this problem, because Object.is('dark', 'dark') is true.
Common errors
- Expecting
memoto block context updates. It does not.memoonly compares props. - Expecting a consumer to re-render only for the field it uses. If the value is an object and any part changes, all consumers re-render.
- Mutating the value. If you change
user.namein place, the reference stays the same. React sees no change and consumers do not update. Always create a new object or array.
Exercise
A Provider passes value={{ count, label }} and is inside a component with unrelated state. A consumer reads only label. The unrelated state changes while count and label stay the same. Does the consumer re-render, and why?
Show answer
Yes, if the Provider's parent re-renders and creates the value object inline. The new object has a different reference, so Object.is reports a change, and every consumer re-renders even though count and label are unchanged. Memoizing the object with useMemo would prevent it.