Sub topic 9
Cheaper fixes: move state down and pass children
This chapter assumes you know how useState triggers re-renders, how props and children work, and that a parent re-render re-renders its children by default. Before reaching for memo, useMemo or useCallback, check whether the state lives in the wrong place. Fixing that costs no comparisons and no dependency arrays.
The problem: state sits too high
When a component's state changes, React re-renders that component and every component it returns, unless something stops it. If fast-changing state such as a text input lives in a parent, all the siblings re-render on each keystroke, even though they never use that state.
import { useState } from 'react';
export function Page() {
const [query, setQuery] = useState('');
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<ExpensiveTree />
</>
);
}
function ExpensiveTree() {
// Imagine a large subtree here.
return <div>Large content</div>;
}Every keystroke updates query, so Page re-renders, and ExpensiveTree re-renders with it. Nothing in ExpensiveTree depends on query.
Fix 1: move state down
Put the state in the smallest component that needs it. Here only the input uses query, so give the input its own component.
import { useState } from 'react';
export function Page() {
return (
<>
<SearchBox />
<ExpensiveTree />
</>
);
}
function SearchBox() {
const [query, setQuery] = useState('');
return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
}
function ExpensiveTree() {
// Imagine a large subtree here.
return <div>Large content</div>;
}Typing now re-renders only SearchBox. Page has no state, so it does not re-render, and ExpensiveTree is untouched. No memo is needed.
Fix 2: pass the slow part as children
Sometimes the state cannot move down because the stateful element wraps the slow content. Here a wrapper holds a color that styles everything inside it.
import { useState } from 'react';
export function ColorScope({ children }) {
const [color, setColor] = useState('red');
return (
<div style={{ color }}>
<input value={color} onChange={(e) => setColor(e.target.value)} />
{children}
</div>
);
}
export function App() {
return (
<ColorScope>
<ExpensiveTree />
</ColorScope>
);
}
function ExpensiveTree() {
// Imagine a large subtree here.
return <div>Large content</div>;
}The <ExpensiveTree /> element is created in App, not in ColorScope. When ColorScope updates its own state, App does not re-render, so the children prop still holds the same element object from the last App render. React sees an identical element and skips that subtree. This is referential equality working for you without any hook.
Choosing a fix
Use this order for three reasons:
- Both structural fixes remove the work. Memoization still runs a props comparison on every parent render.
- They add no dependency arrays that can go stale.
- They keep components simple and easy to read.
Common errors
- Moving state down, then lifting it again. If a sibling later needs the state, you must lift it back up. Decide based on who reads the state today.
- Creating children inside the stateful component. Writing
<ExpensiveTree />insideColorScopemakes a new element on every render, so the optimization disappears. - Skipping the Profiler. Confirm with the React DevTools Profiler that the expensive component no longer renders on each keystroke.
Exercise
A Layout component holds const [open, setOpen] = useState(false) for a sidebar toggle button, and renders <Sidebar open={open} /> plus a large <Report /> that ignores open. Typing in the toggle is fine, but Report re-renders on every toggle. Name two ways to stop that without memo.
Show answer
Move the state into a smaller component that holds the toggle button and the Sidebar, so Layout and Report no longer re-render when open changes. Alternatively, keep the state in a wrapper that accepts children, and have the parent of Layout create <Report /> and pass it as a child, so the same element reference is reused when open changes.