Sub topic 8
Fixing renders by moving state down
This chapter assumes you can read a Profiler recording and know that a state update re-renders the component that owns the state and all of its children. If you have not recorded a session yet, review the earlier chapters on the DevTools Profiler first.
Why moving state helps
When state changes, React re-renders the component that owns it. It then re-renders every child that component returns, whether or not those children use the state. If a fast-changing value, such as the text in an input, lives high in the tree, every keystroke re-renders everything below it.
Moving the state into a smaller component limits the re-render to that component's subtree. This is called state colocation: keep state as close as possible to the components that use it. It removes the extra renders without adding any caching logic.
A worked example
Here is a page where typing in the input re-renders a slow component. The ExpensiveTree stub below does deliberately slow work so the problem shows up in the Profiler.
import { useState } from "react";
function ExpensiveTree() {
const start = performance.now();
while (performance.now() - start < 50) {
// Simulate 50ms of rendering work.
}
return <p>Expensive content</p>;
}
export default function SlowPage() {
const [query, setQuery] = useState("");
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<ExpensiveTree />
</>
);
}Record a few keystrokes. In the Profiler, ExpensiveTree appears in every commit, and the "Why did this render?" panel says the parent component rendered. The fix is to move query into its own component:
import { useState } from "react";
function ExpensiveTree() {
const start = performance.now();
while (performance.now() - start < 50) {
// Simulate 50ms of rendering work.
}
return <p>Expensive content</p>;
}
function SearchBox() {
const [query, setQuery] = useState("");
return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
}
export default function FastPage() {
return (
<>
<SearchBox />
<ExpensiveTree />
</>
);
}FastPage no longer has state, so it never re-renders after mount. Typing re-renders only SearchBox. Record again and compare: the commits should be a fraction of the earlier duration.
When the state is needed higher up
Sometimes the state wraps the slow part, for example a theme or a scroll position that styles a container. You can still avoid the re-render by passing the slow part as children. React keeps the same element between renders, so it skips re-rendering it.
import { useState } from "react";
function ExpensiveTree() {
const start = performance.now();
while (performance.now() - start < 50) {
// Simulate 50ms of rendering work.
}
return <p>Expensive content</p>;
}
function ThemeShell({ children }) {
const [dark, setDark] = useState(false);
return (
<div className={dark ? "dark" : "light"}>
<button onClick={() => setDark((d) => !d)}>Toggle theme</button>
{children}
</div>
);
}
export default function App() {
return (
<ThemeShell>
<ExpensiveTree />
</ThemeShell>
);
}App creates the <ExpensiveTree /> element. ThemeShell re-renders when dark changes, but its children prop is the same element object as before, so React reuses the previous result.
Common mistakes
- Moving state down while a child still needs it. If two siblings both need the value, lift it only as far as their closest common parent.
- Passing the slow component as a prop created inline, such as
<Shell content={<ExpensiveTree />} />inside a component that itself re-renders. The element is then recreated on every render, and the benefit is lost. - Treating this as a replacement for measuring. Confirm in the Profiler that the slow component no longer appears in the commits.
Exercise
A Dashboard component holds const [hover, setHover] = useState(null) and renders a Chart that takes 80ms to render, plus a Tooltip that is the only user of hover. Moving the mouse makes the page stutter. How would you fix it without using memo, and how would you check the fix?
Show answer
Move the hover state into a component that renders the Tooltip (or into Tooltip itself), so Dashboard and Chart no longer re-render on mouse movement. If the hover handlers must sit on an element that wraps Chart, make that wrapper own the state and pass Chart as children. Then record the same mouse movement in the Profiler before and after the change. Chart should be absent from the commits, and the commit durations should drop.