# Concurrency in React 18

> How React 18 introduced interruptible rendering with useTransition and useDeferredValue to keep your app responsive during heavy updates.

Subject: React | Lesson 35 of 48 | 6 min read
Interactive lesson: https://codewithanimation.in/react/concurrent-features
Previous: [List Virtualization](https://codewithanimation.in/react/list-virtualization.md) | Next: [Client-Side Routing](https://codewithanimation.in/react/client-side-routing.md)

## The Synchronous Rendering Problem

Before React 18, rendering was entirely synchronous. Once React started rendering an update (like setting state), it could not stop until it was finished. It blocked the browser's main thread.

If you typed into a search input, and that search input triggered a heavy data visualization that took 500ms to render, your browser would freeze for 500ms. You couldn't even type the next letter.

React 18 introduced **Concurrent Rendering**. React can now pause a heavy render, handle an urgent user event (like typing or clicking), and then resume the heavy render later.

## Urgent vs Non-Urgent Updates

Concurrency introduces the concept of priority. Not all state updates are equally important.

**Urgent updates** reflect direct interaction, like typing, clicking, or dragging. Users expect these to be instant. If they aren't, the app feels broken.

**Non-urgent updates** (Transitions) transition the UI from one view to another, like filtering a massive list or switching pages. Users are okay with a slight delay here, as long as the app doesn't completely freeze.

## useTransition and useDeferredValue

React provides two main hooks to opt-in to concurrent features:

`const [isPending, startTransition] = useTransition();`

Wrap a non-urgent state update inside `startTransition`. React will process it in the background, keeping the main thread free. The `isPending` boolean lets you show a loading indicator.

Alternatively, use `const deferredQuery = useDeferredValue(query);`. This tells React to use the old value for a heavy component until the new value is ready to be rendered in the background.

## Common mistake: Concurrency is not Multi-Threading

A common misconception is that React Concurrent Mode uses Web Workers or runs code on multiple threads. It does not. JavaScript is single-threaded.

React achieves concurrency through cooperative scheduling. It breaks the massive rendering task into tiny micro-tasks and yields back to the browser in between them to see if any urgent events occurred.

## Tip: Don't overuse it

You do not need `useTransition` for every state update. In fact, most of your app should remain synchronous. Only use concurrent hooks when you have definitively proven that a specific state update is causing main-thread freezing that you cannot solve via memoization or pagination.

## Try it: The Main Thread Scheduler

Compare what happens when a user types into an input field that also triggers a massive UI re-render. In Sync mode, the main thread is blocked. In Concurrent mode, the work is yielded, keeping the input fast.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/concurrent-features

## Check yourself

### Question 1

What happens in React 17 (synchronous rendering) when a state update takes 500ms to render?

- A. React automatically defers the render until the user stops interacting.
- B. The browser's main thread is blocked for 500ms, making the page completely unresponsive.
- C. React moves the render to a Web Worker.

Correct answer: B

Synchronous rendering cannot be interrupted. The browser cannot paint frames or process click/typing events until the JavaScript execution finishes.

### Question 2

When should you use `startTransition`?

- A. For updating the value of a text `<input>` as the user types.
- B. For fetching data from an API.
- C. For a state update that triggers a heavy, non-urgent UI change (like filtering a massive list).

Correct answer: C

`startTransition` marks an update as low-priority, allowing React to interrupt it if the user types or clicks something else.

- Why not A: Typing is an urgent update.

### Question 3

How does React 18 achieve Concurrent Rendering?

- A. By spawning multi-threaded Web Workers.
- B. By breaking rendering work into small chunks and yielding back to the browser's main thread between chunks.
- C. By using the GPU to render components instead of the CPU.

Correct answer: B

React is still single-threaded. It achieves concurrency by pausing its own work, checking if the browser needs to handle user input or paint a frame, and then resuming.

## Remember this

- Before React 18, all rendering was synchronous and un-interruptible.
- Concurrent rendering allows React to pause heavy renders to handle urgent user events (like typing).
- Use `startTransition` to mark a state update as non-urgent.
- Use `useDeferredValue` to defer updating a value passed into a heavy component.
- Concurrency is cooperative scheduling on the main thread, not multi-threading.
