Sub topic 11
Choosing between transitions, deferred values and debouncing
This chapter assumes you have read the earlier chapters on useTransition, startTransition and useDeferredValue. It also assumes you are comfortable with useState, props and React.memo. Here you will compare the three tools and pick one for a given problem.
What each tool does
All three tools help when typing or clicking feels slow. They solve different problems.
- A transition marks a state update as non-urgent. React renders it in the background and can interrupt that render if urgent input arrives.
- A deferred value is a copy of a value that lags behind the original. React first renders with the old copy, then re-renders in the background with the new one. The background render can also be interrupted.
- Debouncing waits until the user stops for a fixed time before reacting. It reduces how often the work happens.
Transitions and deferred values do not reduce the amount of rendering work. They change when it happens and let it be abandoned. Debouncing reduces the amount of work, but it adds a fixed delay.
Decision flow
Comparing them
| Question | Transition | Deferred value | Debounce |
|---|---|---|---|
| Where do you apply it? | Around the `setState` call | On the value you read | Around the effect or handler |
| Reduces work? | No | No | Yes |
| Fixed delay? | No, adapts to the device | No, adapts to the device | Yes, for example 300 ms |
| Interruptible render? | Yes | Yes | No |
| Gives an `isPending` flag? | Yes | No, compare the value to its deferred copy | You build it yourself |
| Helps with network calls? | No | No | Yes |
When to pick each one
Use a transition when you own the code that sets the state, such as a tab switch or a filter. It also gives you isPending for free.
Use a deferred value when you do not own the update. A value that comes from a prop, a context or a third-party hook cannot be wrapped in startTransition. Deferring it on the receiving side has the same effect. Memoize the child that receives the deferred value. Otherwise it re-renders with the urgent value anyway, and nothing is saved.
Use debouncing when the cost is outside rendering. Every keystroke that triggers a fetch still sends a request, however React schedules the render. A transition does not delay the request, and a deferred value does not either.
Combining them
The techniques work together. Debounce the request, then defer the rendering of the results. This hook debounces any value:
import { useState, useEffect } from 'react';
export function useDebouncedValue(value, delayMs) {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const id = setTimeout(() => setDebounced(value), delayMs);
return () => clearTimeout(id);
}, [value, delayMs]);
return debounced;
}In a component, you would call useDebouncedValue(query, 300) and fetch with the result. The list that displays the response can then use useDeferredValue if rendering it is also heavy.
Common mistakes
- Wrapping a controlled input's own
setStatein a transition. The text then lags behind the keys. Update the input urgently and put only the expensive state in the transition, as in the search example from earlier. - Using a deferred value without
memoon the child. The child re-renders on every urgent change. - Expecting a transition to cancel a
fetch. It only affects React rendering. - Reaching for debouncing when the slowness is only in rendering. You add latency that a transition would not.
Exercise
A search box sends a request to a server on every keystroke, and the returned list of 5,000 rows is slow to render. Which tools would you use, and for which part of the problem?
Show answer
Debounce the query (for example with useDebouncedValue) so fewer requests are sent, because only debouncing reduces network calls. Then render the results with useDeferredValue, or set the results state inside a transition, with the list wrapped in memo, so that typing stays responsive while the 5,000 rows render in the background.
Exercise
A child component receives query as a prop from a parent you cannot edit. You want its slow list to lag behind typing. Do you use useTransition or useDeferredValue?
Show answer
useDeferredValue. You do not control the setState call in the parent, so you cannot wrap it in a transition. Defer the prop value inside the child and memoize the slow list.