useTransition and useDeferredValue

Intermediate · 16 min

useTransition's pending state for marking your own updates non-urgent, and useDeferredValue for deferring a value you only receive as a prop.

Before thisConcurrent rendering and transitionsuseState and component state

Academind · 16 min

useTransition and useDeferredValue

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by AcademindReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • useTransition wraps startTransition and adds an `isPending` flag, so a component can show its own loading indicator for a non-urgent update without wiring up separate state.
  • Call useTransition when you control the update yourself — you own the state setter and can choose to call it inside the transition function it gives you.
  • useDeferredValue solves a different problem: you don't own a setter, you only have a value (often a prop or another hook's return value) that changes quickly, and you want a lagging copy of it for expensive work.
  • useDeferredValue returns a version of the value that can lag behind the real one during urgent updates, then catches up once React has spare rendering capacity.
  • The practical test for which one to reach for: if you're calling setState yourself, use useTransition; if you're just receiving a fast-changing value, use useDeferredValue.
  • Both leave the fast-changing part of the UI (the input you're typing into) rendering at full, uninterrupted priority — the whole point is to keep that part responsive while the slow part catches up.

Two hooks for the same underlying mechanism

The previous lesson introduced startTransition as the raw entry point for marking an update as non-urgent. useTransition and useDeferredValue are the two hooks most apps actually reach for, and they solve two different shapes of the same problem: keeping an expensive re-render from blocking something the user is actively doing, like typing.

The difference between them comes down to one question: do you own the state update, or are you only handed a value?

useTransition: when you own the setState call

useTransition returns a tuple — [isPending, startTransition] — where startTransition here is a transition function scoped to this component, and isPending tracks whether that transition is still running.

import { useState, useTransition } from "react";

function TabContainer() {
  const [tab, setTab] = useState("home");
  const [isPending, startTransition] = useTransition();

  function selectTab(nextTab) {
    startTransition(() => {
      setTab(nextTab); // expensive: re-renders a heavy tab panel
    });
  }

  return (
    <>
      <TabButton onClick={() => selectTab("home")}>Home</TabButton>
      <TabButton onClick={() => selectTab("posts")}>Posts</TabButton>
      {isPending && <Spinner />}
      <TabPanel tab={tab} />
    </>
  );
}

Clicking a tab button still fires immediately — the click itself is never delayed. What’s deprioritized is the re-render that follows from setTab, which could be expensive if TabPanel renders a lot of content. isPending turns true while that re-render is pending and false once it commits, giving you a ready-made loading signal without a second useState call to manage it by hand.

This is the case useTransition is built for: you control the setter, so you can choose exactly which updates get wrapped.

useDeferredValue: when you only have a value

Sometimes there’s no setState to wrap. A component receives a value — often from a parent, or from a fast-updating piece of state elsewhere — and needs to do something expensive with it on every change, with no transition boundary of its own to create.

import { useState, useDeferredValue, useMemo } from "react";

function SearchableList({ allItems }) {
  const [query, setQuery] = useState("");
  const deferredQuery = useDeferredValue(query);

  const results = useMemo(
    () => filterItems(allItems, deferredQuery),
    [allItems, deferredQuery],
  );

  return (
    <>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      <ResultsList items={results} />
    </>
  );
}

setQuery is an ordinary, unmarked update — the input stays at full priority and updates on every keystroke with no lag. deferredQuery, by contrast, is allowed to lag behind query: while React is busy with something more urgent (the next keystroke), deferredQuery keeps its previous value, so the expensive filterItems call reruns against stale input rather than blocking the keystroke from landing. Once there’s spare rendering capacity, React re-renders SearchableList with deferredQuery caught up to the latest query, and the list updates.

Note what didn’t change here: there is still only one setQuery call, and it is not wrapped in anything. useDeferredValue operates on the read side — it gives you a second, lagging view of a value you already have — rather than on the write side.

The worked contrast

Put side by side, the two approaches answer the same need differently:

  • With useTransition, you say “this specific state update I’m about to make is non-urgent” — useful when a click or a selection triggers expensive work and you’re the one calling the setter.
  • With useDeferredValue, you say “this value changes quickly, and I want a lagging version of it for expensive downstream work” — useful when you’re just a consumer of a value, like a search input’s current text, that’s set somewhere else (or by a plain, unmarked setState).

A search-as-you-type filter is usually solved with useDeferredValue, because the input’s own setQuery call needs to stay fast and unmarked — you don’t want the typing itself demoted. Reach for useTransition instead when the trigger is more like a button click or a tab switch, where there’s a discrete action you control and want to mark as non-urgent at the source.

What the video covers

Academind’s video explains both hooks in a single, coherent walkthrough and builds the same kind of contrast used above — when the pending-state, setState-wrapping hook (useTransition) fits versus when deferring a received value (useDeferredValue) fits — which is unusually tidy for two APIs that are easy to conflate. It’s a solid primary source for both halves of this lesson in one sitting; the shorter Web Dev Simplified link below covers just the useTransition half if you want a faster second pass on that piece alone.

Where this fits

Both of these hooks assume a component is already rendering normally and just needs to deprioritize part of its own update. The next lesson covers a hook that changes a much older rule — where in your code you’re even allowed to call a hook: the use hook.

Check yourself

  1. 01A component owns the state that drives an expensive re-render and calls its own setState to update it. Which hook is the more direct fit, useTransition or useDeferredValue?

    useTransition. It's designed for exactly this case — you control the state update yourself, so you can wrap the setState call in the transition function useTransition gives you, and get an isPending flag back for free to show a loading indicator while that update is in flight.

  2. 02A component receives a fast-changing value as a prop and has no setState call of its own to wrap in a transition. What should it use instead?

    useDeferredValue. Since there's no update to mark as a transition — the value just arrives from outside — useDeferredValue instead gives the component a lagging copy of that value to do its expensive work against, letting the value catch up once rendering capacity frees up.

  3. 03What does the isPending boolean that useTransition returns actually tell you?

    Whether the transition's update is still being processed in the background. It lets you render a loading indicator (a spinner, a dimmed section, and so on) for the non-urgent update without creating and managing a separate boolean state variable yourself.

  4. 04In a search-as-you-type filter, what specifically stays instantly responsive when the filtered list is wrapped in useDeferredValue?

    The input field itself. Its value updates on every keystroke at normal, uninterrupted priority; only the expensive work that depends on the deferred value — re-rendering the filtered list — is allowed to lag behind and catch up once React has a free moment, so typing never feels blocked by the list re-render.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark