Sub topic 8
Why the child must be memoized
This chapter assumes you know function components, useState, props, and how React re-renders a component tree. It builds on the previous chapter, which introduced useDeferredValue. Here you will see why that hook often does nothing useful unless the child component is wrapped in React.memo.
What useDeferredValue does on each render
useDeferredValue(value) returns a value that lags behind the real one. When value changes, React first re-renders with the old deferred value. This first pass is urgent and fast. Then React starts a second render in the background with the new value. If more input arrives, React can abandon that background render and start over.
So a single state change causes up to two renders of the parent:
- An urgent render where
queryis new anddeferredQueryis still old. - A background render where both are new.
The problem without memo
In the urgent render, the parent component runs again. By default, when a parent re-renders, React re-renders all of its children, even if their props did not change.
Suppose SlowList is not memoized. In the urgent render the parent passes deferredQuery, which still has the old value. The props are identical, but React renders SlowList anyway. That is the slow work you wanted to avoid, and it now blocks the keystroke. The input feels just as laggy as before, and you also pay for the background render.
The fix: React.memo
memo tells React to compare the new props with the previous props and skip the render if they are equal. Here is the example from this tutorial:
import { useState, useDeferredValue, memo } from 'react';
const SlowList = memo(function SlowList({ query, items }) {
const visible = items.filter((item) => item.includes(query));
return (
<ul>
{visible.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
);
});
function App({ items }) {
const [query, setQuery] = useState('');
const deferredQuery = useDeferredValue(query);
const isStale = query !== deferredQuery;
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<div style={{ opacity: isStale ? 0.6 : 1 }}>
<SlowList query={deferredQuery} items={items} />
</div>
</>
);
}Follow one keystroke through this code:
- In the urgent render,
queryis new anddeferredQueryis old.SlowListreceives the samequeryand the sameitems.memosees equal props and skips it. The input updates immediately. - In the background render,
deferredQuerycatches up. The props differ, soSlowListrenders. If you type again first, React throws this render away. isStaleis true between the two renders, so the list is dimmed to show it is out of date.
Common mistakes
- Forgetting memo. The code works but feels no faster. This is the most common reason a deferred value seems to have no effect.
- Passing the live value too. If you pass both
queryanddeferredQueryto the child, the live value changes in the urgent render and defeatsmemo. Pass only the deferred value to the slow part. - Unstable props. Inline objects, arrays, or functions change identity on every render and break the comparison.
- Memoizing the wrong component. Memoize the component that does the slow work, not the parent that owns the state.
How this differs from transitions
With startTransition, you mark the state update, and React already knows that the resulting render is low priority. With useDeferredValue, you mark a value, and the urgent render still happens with the old value. The urgent render is the part that needs memo to be cheap.
Exercise
A search page calls useDeferredValue(query) and passes deferredQuery to a slow ResultList that is not wrapped in memo. Typing still feels laggy. Why, and what is the smallest fix?
Show answer
The urgent render re-renders the parent, and React re-renders ResultList too because it is not memoized, even though deferredQuery still has its old value. The slow render therefore blocks typing. Wrap ResultList in memo so React skips it when its props are unchanged during the urgent render. Also make sure every other prop passed to it keeps a stable identity.
Exercise
You wrap SlowList in memo, but you pass it query={deferredQuery} and onSelect={(item) => setSelected(item)} written inline in the parent. Does memo still help? Why?
Show answer
No. The inline onSelect function is a new object on every render, so the props are never equal and SlowList re-renders during the urgent render. Wrap the handler in useCallback, or pass setSelected directly, so its identity stays stable.