TopicDetails loads one topic, chosen by its id prop. Because id is in the
dependency list, choosing another topic loads that one. This effect waits with
.then rather than await: it has one step and no error handling, so it
doesn't need an async function. It means the same as the previous lesson's
await version.
useEffect(() => {
setTopic(null);
fetchTopic(id).then((data) => {
setTopic(data);
});
}, [id]);npm install and npm run dev. The pretend server answers at different speeds: Components takes 1.2 seconds, State 0.6 and Effects 0.2. Real servers vary like this too.
Predict: two quick clicks
Click Components, then Effects straight away, before Components has finished loading. Which topic is highlighted, and which details do you see after a couple of seconds?
Check your prediction
The Effects button is selected, but after a moment the card shows Components. Both requests were running. Effects answered first and was shown, then the slow Components answer arrived and replaced it. The page now disagrees with itself.
This is called a race condition: the result depends on which request wins the race. You can't control how fast a server answers, so the component has to ignore answers it no longer wants.
Ignore stale answers with cleanup
useEffect(() => {
let ignore = false;
setTopic(null);
fetchTopic(id).then((data) => {
if (!ignore) setTopic(data);
});
return () => {
ignore = true;
};
}, [id]);Each run of the effect has its own ignore variable. When id changes, React
runs the old effect's cleanup first, which sets that run's ignore to
true. Its answer may still arrive, but it's dropped. Only the latest request
can update the page.
Make the change in TopicDetails.jsx and repeat the two quick clicks. The
details now always match the highlighted button.
The same idea everywhere
This is the pattern the last lesson was missing too. Add let ignore and a
cleanup to any effect that loads data, even when a race seems unlikely. On your
own computer, StrictMode's double run of effects relies on the cleanup in the
same way.
On real projects, libraries such as TanStack Query handle loading, caching and races for you. Knowing the pattern underneath tells you what those libraries are doing.
Why not just check the id?
Someone suggests a different fix: inside .then, compare data.id with id
and only call setTopic if they match. Does that work? Explain using what each
render's id is.
Hint 1: the idea
A callback keeps the values of the render that created it. Comparing two values from the same old request cannot tell you which request is current.
Hint 2: a first step
Which id does the .then callback see: the one from when the request started,
or the latest one?
Show one solution
It doesn't work. The callback belongs to the render that started the request,
so its id is that render's id, and the slow answer always matches it.
data.id === id is true for the stale answer too. Only something the newer
render can change, such as the cleanup setting ignore, can tell the old
request that it's out of date.
Completion is saved in this browser only. It does not measure mastery.