How does useDeferredValue help improve performance when rendering a large list based on user input?
Short answer
useDeferredValue lets you keep showing an older, slightly outdated version of a value while React works on rendering the newer version in the background.
Advertisement
AModel answer
useDeferredValue lets you keep showing an older, slightly outdated version of a value while React works on rendering the newer version in the background. This means the input field stays smooth and responsive, while the heavier part of the screen, like a large filtered list, catches up a moment later.
function SearchResults({ query }) {
const deferredQuery = useDeferredValue(query);
const results = useMemo(() => filterHugeList(deferredQuery), [deferredQuery]);
return <ResultsList items={results} />;
}Real-world example
A data table with ten thousand rows stays smooth while a user types quickly into a filter box, since useDeferredValue lets the heavy filtering work catch up slightly behind the user's typing instead of blocking it.
Likely follow-ups
- How is useDeferredValue different from useTransition, since they solve a similar problem?
- How do you show a visual hint to the user that the results are slightly behind what they typed?
Related topics