Understanding React Concurrency
Published: 2023-05-29 10:35:07

---
title: Understanding React Concurrency
published: true
description: 
tags: 
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-05-29 10:34 +0000
---


React v18.0 has broken ground by introducing a long-awaited feature: Concurrency! Concurrency refers to having more than one task in progress at once, and concurrent tasks can overlap depending on which is more urgent.

---

## What is concurrency?

It is a way to structure a program by breaking it into pieces that can be executed independently. This is how we can break the limits of using a single thread, and make our application more efficient.

The basic premise of React concurrency is to re-work the rendering process such that while rendering the next view, the current view is kept responsive.

Concurrent Mode was a proposal the React team had to improve application performance. The idea was to break up the rendering process into interruptible units of work.

Under the hood, this would be implemented by wrapping component renders in a requestIdleCallback() call, keeping applications responsive during the rendering process.

So hypothetically, if Blocking Mode were implemented like this:

```javascript
const renderBlocking = (Component) => {
    for (let Child of Component) {
        renderBlocking(Child);
    }
}
```

Then Concurrent Mode would be implemented like this:

```javascript
const renderConcurrent = (Component) => {
    // Interrupt rendering process if state out-dated
    if (isCancelled) return;

    for (let Child of Component) {
        // Wait until browser isn't busy (no inputs to process)
        requestIdleCallback(() => renderConcurrent(Child));
    }
}
```

If you’re curious how React does this in reality, take a peek at the [implementation of React’s scheduler package](https://github.com/facebook/react/blob/5309f102854475030fb91ab732141411b49c1126/packages/scheduler/src/forks/Scheduler.js#L209-L277).
After initially using requestIdleCallback, React switched to requestAnimationFrame, and later to a user-space timer.

---

## Good bye to (Concurrent) Mode, Only Features

The Concurrent Mode plan did not materialize for backward-compatibility reasons.

Instead, the React team pivoted to Concurrent Features, a set of new APIs selectively enabling concurrent rendering. So far, React has introduced two new hooks to opt into a concurrent render.
- startTrnsition
- useTransiiton
- useDefferedValue

---

Lets now understand all of them with examples

## startTransition
The startTransition hook introduced with React 18 helps us keep our app responsive without blocking your user interactions by allowing you to mark specific updates as transitions.

There are two categories of state updates in React:

- Urgent updates: show direct interaction like clicking, typing, etc.
- Transition updates: change UI views

React considers state updates wrapped in startTransition as non-urgent, so they can be suspended or interrupted by urgent updates.

For example, as a user, it would feel more natural to see the letters as you type in a search input field for filtering data, but as expected, the search result may take a while, and that’s OK.

```javascript
 import { startTransition } from 'react';

// Urgent
setInputValue(input);
    
// Mark any state updates inside as transitions
startTransition(() => {
   // Transition
   setSearchQuery(input);
})
```

## useTransition

React can also track and update pending state transitions using the useTransition hook with an isPending flag. This lets you display loading feedback to your users, letting them know that work is happening in the background.

The useTransition hook returns two items:

- Boolean flag isPending, which is true if a concurrent render is in progress
- Function startTransition, which dispatches a new concurrent render

To use it, wrap setState calls in a startTransition callback.

```javascript
const MyCounter = () => {
    const [isPending, startTransition] = useTransition();
    const [count, setCount] = useState(0);
    const increment = useCallback(() => {
        startTransition(() => {
            // Run this update concurrently
            setCount(count => count + 1);
        });
    }, []);
 
    return (
        <>
            <button onClick={increment}>Count {count}</button>
            <span>{isPending ? "Pending" : "Not Pending"}</span>
            // Component which benefits from concurrency
            <ManySlowComponents count={count} />
        </>
    )
}
```

Conceptually, state updates detect if they are wrapped in a startTransition to decide whether to schedule a blocking or concurrent render.

```javascript
const startTransition = (stateUpdates) => {
  isInsideTransition = true;
  stateUpdates();
  isInsideTransition = false;
}

const setState = () => {
  if (isInsideTransition) {
    // Schedule concurrent render
  } else {
    // Schedule blocking render
  }
}
```

An important caveat of useTransition is that it cannot be used for controlled inputs. For those cases, it is best to use useDeferredValue.

---

## useDeferredValue
The useDeferredValue hook is a convenient hook for cases where you do not have the opportunity to wrap the state update in startTransition but still wish to run the update concurrently.

useDefferedValue takes in a state value and a timeout in milliseconds and returns the deferred version of that state value. The timeout tells React how long it should delay the deferred value.

Conceptually, useDeferredValue is a debounce effect and can be implemented as such:

```javascript
import { useState, useDeferredValue } from "react";
 
   const App = () => {
      const [input, setInput] = useState("");
      const deferredValue = useDeferredValue(text, { timeoutMs: 3000 }); 
    
      return (
        <div>
          <input value={input} onChange={handleChange} />
          <MyList text={deferredValue} />
        </div>
      );
 }
```

With what we have in the code snippet above, the input value will be displayed immediately when a user starts typing, but useDeferredValue will display a previous version of the MyList component for at most 3000 milliseconds.

### Concurrent Features & Suspense
The useTransition and useDeferredValue hooks have a purpose beyond opting in concurrent rendering: they also wait for Suspense components to complete.

---

If you like this post, [you can buy me a coffee](https://www.buymeacoffee.com/ArchitectAK).

<a href="https://www.buymeacoffee.com/ArchitectAK"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BSV4goFV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zir1erzg7v28nvyuue09.jpg" /></a>

Also, to be notified about my new articles and stories: Follow me on [Medium](https://architectak.medium.com/).

Subscribe to my [YouTube Channel](https://www.youtube.com/TechTalksWithAK) for educational content on similar topics

Follow me on [Hashnode](https://hashnode.com/@ArchitectAK) and [GitHub](https://github.com/ArchitectAK), for connecting quickly

You can find me on [LinkedIn](https://www.linkedin.com/in/ArchitectAK) as it’s a professional network for people like me and you.

Cheers!!!!