Timers, cleanup, and tests for a hook that is easy to get subtly wrong. React keeps changing how it wants us to think about rendering, and each change removes a class of bugs if you adopt the new model fully instead of translating old habits.
The mental model
Think of a component as a description of what the screen should look like for the current data. Effects synchronise that description with things outside React, such as timers, subscriptions, or the document title.
import { useEffect, useState } from 'react';
export function useDebouncedValue<T>(value: T, delay = 300): T {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const id = setTimeout(() => setDebounced(value), delay);
return () => clearTimeout(id);
}, [value, delay]);
return debounced;
}
Putting it into a component
The mistakes we see most often are effects that compute data which could have been derived during render, and state that duplicates props. Both lead to screens that are briefly wrong after every update.
- Derive values during render instead of storing them.
- Give every effect a cleanup function.
- Keep state as close to where it is used as possible.
- Put Suspense boundaries where users expect a loading state.
Common mistakes
Server rendering and streaming add a second axis: where the component runs. Keep data fetching on the server when you can, and ship only the interactive pieces to the browser.
We have been running this approach for several months now. The initial setup took about a day, and the ongoing cost is close to zero once the team knows where to look.
None of this is new, and that is the point. The techniques that last are usually the boring ones that are documented well and supported by the platform itself.
As always, the examples are simplified to fit on a page. The production version has more error handling and logging, but the shape of the code is the same.
Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.
We will revisit this in six months with fresh numbers. If the results change, we will update the post and note what changed at the end.
If you try this on your own project, start with one page or one service rather than the whole codebase. A small experiment gives you real numbers to argue with, and it is easy to roll back if the idea does not fit.
Before you ship this
Strict Mode mounts components twice in development. If your effect breaks when that happens, it is missing a cleanup, not suffering from a React bug.
Simplicity is prerequisite for reliability.
Edsger W. Dijkstra, EWD498, 1975
Wrapping up
Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.
The part about naming things made me laugh, then made me go rename three functions.
The quote in the middle is doing a lot of work here, and it earns it.
I would push back a little on the second section. In our monorepo the trade-off went the other way.
Agreed. Start simple and only reach for this when you feel the pain.
Glad it helped!
Our accessibility audit flagged exactly this last month. I wish I had read this first.
Any thoughts on how this interacts with server-side rendering?
Came for the snippet, stayed for the explanation.
Any thoughts on how this interacts with server-side rendering?
Great post. Did you measure the impact before and after, or is this mostly from experience?
Solid write-up. The warning about caching bit us hard last year.