Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReactPerformance

Choose useTransition or useDeferredValue

Use useDeferredValue to defer a value you receive and useTransition to mark a state update you control as non-urgent.

You type into a search box and every keystroke freezes the page for a moment. The input itself is cheap. Filtering and rendering the list under it is the slow part, and each keystroke waits for it.

React 18 has two hooks for this, and they look interchangeable. Both tell React that the list can wait while the input cannot.

You have the value: useDeferredValue

useDeferredValue lets a slow consumer keep using the old value during an urgent render. React tries rendering the consumer with the new value in the background.

import { memo, useDeferredValue, useState } from 'react'

function App() {
  const [query, setQuery] = useState('')
  const deferredQuery = useDeferredValue(query)
  return (
    <>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      <SlowList query={deferredQuery} />
    </>
  )
}

const rows = Array.from({ length: 5000 }, (_, index) => `Item ${index}`)

const SlowList = memo(function SlowList({ query }) {
  return <ul>{rows.filter((row) => row.includes(query)).map((row) => (
    <li key={row}>{row}</li>
  ))}</ul>
})

I logged each render while typing one letter, and the output is App query="a" deferredQuery="" followed by App query="a" deferredQuery="a". The input updated first, and SlowList rendered once, for "a", after that.

memo lets SlowList skip the urgent render while deferredQuery still has its previous value. A change to another prop would make the list render again.

You have the setter: useTransition

To mark the list update as a transition, reuse the memoized SlowList and replace App with this version. startTransition calls its callback immediately and marks the synchronous state update inside as non-urgent.

import { useState, useTransition } from 'react'

function App() {
  const [query, setQuery] = useState('')
  const [listQuery, setListQuery] = useState('')
  const [isPending, startTransition] = useTransition()

  return (
    <>
      <input
        value={query}
        onChange={(e) => {
          const next = e.target.value
          setQuery(next)
          startTransition(() => setListQuery(next))
        }}
      />
      <SlowList query={listQuery} />
    </>
  )
}

The log for one letter reads query="a" listQuery="" isPending=true, then query="a" listQuery="a" isPending=false. The input is current on the first render, and isPending is true until the list catches up.

The input keeps its own query state outside the transition. The React docs say transition updates can't control a text input, because the input must update synchronously.

How I choose

I ask where the slow render comes from. I use useDeferredValue when a component receives the value as a prop, or when wrapping the setter would mean editing someone else's code. It changes one line at the point of use.

I use useTransition when I own the set call, as with a tab switch or a route change. isPending gives me something to show while the new screen renders.

useDeferredValue adds no fixed delay. A new keystroke can interrupt the background render. Work inside a single expensive synchronous function still blocks until that function returns.