Runs with Vite on your computer: unzip, then npm install and npm run dev.
The letters appear in bursts. Each keystroke changes query, and React
re-renders the whole list before it can show the next letter. In our tests, each
keystroke took around 200 ms to appear. Anything over about 100 ms feels
broken.
Not all updates are equally urgent
The text in the box must update at once: it's what the user is doing. The
list can lag a little behind. React lets you say so.
useDeferredValue
JavaScript
import { useDeferredValue, useState } from "react";
On each keystroke, React first re-renders with the olddeferredQuery.
SlowList is wrapped in memo, so with unchanged props it's skipped, and the
new letter appears straight away. Then React renders again in the background
with the new deferredQuery. If you type another letter first, it abandons
that background render and starts over with the newest value:
Make the change and type quickly again. The input keeps up; the list follows a
moment later. In our tests, keystrokes went from about 200 ms to under 20 ms.
memo matters here. Without it, SlowList would re-render with the urgent
update too, because its parent did, and nothing would improve.
useTransition
useDeferredValue defers a value. useTransition marks a state update
as non-urgent, and tells you when it's in progress:
Use it when you own the slow update, such as switching to a tab whose content is
slow to render. isPending lets you show that something is happening, while the
old tab stays usable. Here Practice takes about a third of a second to
render:
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Click Practice, then Notes straight away. The page freezes: neither
button responds until the whole practice list has rendered, and only then does
Notes open.
Now make the switch a transition. Import useTransition, call it at the top of
App, wrap setTab(next) in startTransition, and show that the switch is in
progress next to the buttons:
Click Practice: Loading… appears at once while Overview stays on
screen, and a moment later the practice list replaces it. Click Practice
then Notes quickly: Notes opens almost straight away. React abandoned
the unfinished practice render, because a newer update arrived.
Deferring rendering doesn't debounce network requests and doesn't move your
JavaScript to a worker. Keep the input's own state update urgent; pass the
deferred value to the expensive view. For requests, Day 5's debouncing and
stale-response handling still matter.
Show that the list is catching up
While the list lags behind, it shows results for an older query. Make that
visible: when query !== deferredQuery, the list is stale.
Exercise
A stale indicator
While the list hasn't caught up, show Updating… next to the search box, and
fade the list to half opacity.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Nothing new needs storing. The list is behind exactly when the value it shows
differs from what's in the box, and both of those values are already available
while App renders. Work out a true/false value from them, then use it twice.
Hint 2: a first step
Start with the text: add the deferred value as in the lab above, then show
Updating… after the input when the two values differ. Type quickly and the
word flickers on and off. For the fade, put the list inside a <div> and give
it an inline style whose opacity depends on the same value.