Keeping the UI responsive with useTransition
Filtering a long list or rendering a heavy screen can take a moment. React commits every update in one go, so while that work runs the browser cannot paint, and the page feels frozen. You tap a button and nothing moves.
useTransition lets you mark an update as non-urgent, so React can keep the
interface responsive:
const [isPending, startTransition] = useTransition();
startTransition(() => {
setName('bulbasaur');
});
It returns two values. startTransition marks the state updates inside its
callback as a transition. isPending is true while React is still working on
that transition, so you can show a pending indicator:
<p>{isPending ? 'Loading…' : name}</p>
In React 19 you can also give startTransition an async function. The
transition stays pending until the returned promise settles, which is useful
when the non-urgent work is a real fetch:
startTransition(async () => {
const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=1');
const data = await response.json();
setName(data.results[0].name);
});
isPending starts false, becomes true when the transition begins, and
returns to false once the transitioned update commits. Your job is to start a
transition, load real data, and show a pending state while it runs.
Pokémon and Pokémon character names are trademarks of Nintendo.
Challenge
A fetch should leave the page responsive while it runs. Export an App component
from App.jsx that starts an async transition when a button labelled Load
is clicked. Inside the transition, fetch the first Pokémon from
https://pokeapi.co/api/v2/pokemon?limit=1 and set a state value to the fetched
name. Show Loading… in a <p> while the transition is pending, and the name
otherwise.
The Test stubs fetch, clicks Load, expects Loading… while the transition
runs, then expects the Pokémon name once it settles. The starter renders
Loading… and never updates, so the Test fails.
Pokémon and Pokémon character names are trademarks of Nintendo.