← Back to React, page 2

react-0038 · Hard senior

What is concurrent rendering in React 18 and later, and what does it change for the way you write components?

Concurrent rendering means React can start rendering an update, pause it, handle a more urgent update, and then resume or discard the unfinished work. Rendering becomes interruptible, but the commit to the DOM is still one synchronous step. It is enabled by createRoot, and you use it through transitions, useDeferredValue and Suspense. It requires components to be pure, because React may call them more than once or throw the result away.

Full answer

Before React 18, once a render started it ran to the end on the main thread. A slow render of a large list could freeze typing, because the browser could not process input until React finished.

With concurrent rendering the render phase can be interrupted. React splits work into small units on its fiber tree and checks between units whether something more urgent arrived. Updates carry a priority (called a lane internally). A click or keystroke is urgent. An update inside startTransition is not. React renders the urgent one first and commits it. It continues the transition afterwards, restarts it if its inputs changed, or abandons it.

Concurrency is not parallelism or multiple threads. There is still one JavaScript thread. It is also not a feature you switch on by itself: using createRoot makes the concurrent renderer available, and behaviour changes only when you use the features that need it.

Those features are transitions (useTransition, startTransition), deferred values (useDeferredValue), Suspense with data and code loading, and streaming server rendering with selective hydration. In React 19 the Activity component builds on the same machinery to pre-render hidden UI at low priority.

The commit phase stays synchronous. DOM updates and layout effects cannot be interrupted, so users never see half of an update.

The cost is a discipline. Render must be pure. A render may run, be thrown away and run again, so it should not write to variables outside the component, mutate props, or perform side effects. StrictMode helps by rendering twice in development. External stores that change during a render can cause "tearing", where parts of the UI show different values. useSyncExternalStore prevents it.

The example logs the two renders caused by one click: the urgent value first, with the transition value still old, and then the transition value.

jsx
import { useState, useTransition } from 'react';

export default function Demo() {
  const [urgent, setUrgent] = useState(0);
  const [heavy, setHeavy] = useState(0);
  const [isPending, startTransition] = useTransition();

  console.log('render urgent=' + urgent + ' heavy=' + heavy + ' pending=' + isPending);

  function handleClick() {
    setUrgent((n) => n + 1);
    startTransition(() => {
      setHeavy((n) => n + 1);
    });
  }

  return (
    <div>
      <button onClick={handleClick}>Update both</button>
      <p>
        urgent: {urgent}, heavy: {heavy}
      </p>
    </div>
  );
}

One click sets an urgent state and a transition state. React renders the urgent update first, with the transition value still old and isPending true, and then renders the transition. Console output (the first line is the mount): render urgent=0 heavy=0 pending=false render urgent=1 heavy=0 pending=true render urgent=1 heavy=1 pending=false

Key points

  • Concurrent rendering makes the render phase interruptible and prioritized. The commit phase stays synchronous.
  • It is available with createRoot and used through transitions, deferred values and Suspense.
  • It is about scheduling on one thread, not parallelism.
  • Components must be pure, because renders can be repeated or discarded.
  • External stores should use useSyncExternalStore to avoid tearing.

Common mistakes

  • Believing React 18 renders in parallel on several threads.
  • Impure renders, such as writing to a module variable, that break when React repeats a render.
  • Expecting concurrency to be active without using any concurrent feature.
  • Reading from a mutable external store in render instead of using useSyncExternalStore.
  • Assuming the DOM can show a half-finished update. The commit is atomic.

Follow-ups

  • What is tearing, and how does useSyncExternalStore prevent it?
  • How does Suspense behave inside a transition compared with an urgent update?
  • What does Activity do with hidden content in terms of priority?

Tips

  • Separate render (interruptible) from commit (synchronous). It is the most precise way to explain it.
  • Say "one thread, scheduled work". Interviewers listen for the parallelism misconception.
  • Tie it to code discipline: purity is the price of the feature.