Sub topic 11
Keeping input responsive with useDeferredValue and useTransition
This chapter assumes you can write function components with state and hooks, and that you have already recorded a Profiler session and found a slow render. Here you will keep typing responsive when a re-render is expensive and cannot be avoided.
The problem: urgent and non-urgent updates
Typing in a search box should feel instant. The results list that depends on the text can take longer. By default, React treats both updates as equally urgent. If the results take 200 ms to render, the input freezes for 200 ms on every keystroke, because React cannot paint the new character until the whole render finishes.
Concurrent rendering fixes this by letting React split updates into two kinds:
- Urgent updates, such as typing, clicking, and pressing keys. These must show up immediately.
- Transition updates, such as filtering a large list. These can wait, and React can interrupt them if something more urgent arrives.
useDeferredValue: defer a value you receive
useDeferredValue takes a value and returns a copy that may lag behind it. Use it when you do not control the code that sets the state, or when you only have the value.
import { useDeferredValue, useState } from "react";
export function Search({ items }) {
const [query, setQuery] = useState("");
const deferredQuery = useDeferredValue(query);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<SlowResults items={items} query={deferredQuery} />
</>
);
}The input uses query, so it updates at once. SlowResults uses deferredQuery. React first re-renders with the old deferred value, which is cheap. Then it renders again in the background with the new value. If you type again before that finishes, React abandons the background render and starts over.
Wrap SlowResults in memo. Without it, the first, urgent render still re-renders SlowResults with the unchanged deferred value, and you gain nothing.
useTransition: mark a state update as non-urgent
useTransition wraps the state update itself. It returns isPending and startTransition. Use it when you own the setState call.
import { useState, useTransition } from "react";
export function Tabs() {
const [tab, setTab] = useState("home");
const [isPending, startTransition] = useTransition();
function selectTab(next) {
startTransition(() => {
setTab(next);
});
}
return (
<>
<button onClick={() => selectTab("posts")}>Posts</button>
<div style={{ opacity: isPending ? 0.6 : 1 }}>
{tab === "posts" ? <SlowPosts /> : <Home />}
</div>
</>
);
}While the transition renders, isPending is true, so you can dim the old content. The screen keeps showing the old UI until the new one is ready.
Choosing between them
| Situation | Use |
|---|---|
| You own the `setState` call | `useTransition` |
| You only receive a value, such as a prop | `useDeferredValue` |
| You need a pending flag | `useTransition` |
Common mistakes
- Deferring a controlled input's own value. Keep the input bound to the urgent state.
- Skipping
memoon the slow child when usinguseDeferredValue. - Expecting these hooks to make a slow render faster. They only keep the page responsive. The total work is the same, or slightly more.
- Putting a controlled input's
setStateinsidestartTransition. Transition updates can lag, which breaks typing.
Exercise
You have a setQuery handler for a text input and an expensive list that filters by query. Which hook do you use, and which component needs memo?
Show answer
Use useDeferredValue(query) and pass the deferred value to the list. The list component needs memo, so it skips the urgent render when its props have not changed. You could also use useTransition with a second state variable for the list's query, so the input state stays urgent.