Philipp Rost / Blog

Understanding Concurrent Mode in React.js

Understanding Concurrent Mode in React.js

Introduction

In this article, we delve into Concurrent Mode in React.js, a feature that allows React to interrupt the rendering process to work on multiple tasks simultaneously. For experienced React developers and beginners alike, understanding Concurrent Mode is essential for building efficient, user-friendly applications.

Concurrent Mode is an opt-in feature in React that improves the user interface's responsiveness by allowing React to interrupt the rendering process to work on multiple tasks at the same time. In traditional React, the rendering process is synchronous and blocking, but Concurrent Mode changes this by making rendering asynchronous and non-blocking.

Why Use Concurrent Mode?

Enhanced User Experience

Concurrent Mode enhances the user experience by ensuring the interface remains responsive, even when performing heavy computations or loading data.

Efficient Resource Utilization

It allows for efficient utilization of resources by prioritizing tasks and ensuring that high-priority updates are not delayed by low-priority ones.

How to Enable Concurrent Mode

In order to use Concurrent Mode, you must use the ReactDOM.createRoot() API.

import ReactDOM from 'react-dom'; import App from './App'; const root = document.getElementById('root'); const concurrentRoot = ReactDOM.createRoot(root); concurrentRoot.render(<App />);

Working with Concurrent Mode

Transition
In Concurrent Mode, you can use the useTransition hook to let your components wait for something before they update.

import React, { useTransition } from 'react'; function App() { const [startTransition, isPending] = useTransition(); let [resource, setResource] = React.useState(null); const handleClick = () => { startTransition(() => { // set resource to some data here }); }; return ( <> <button onClick={handleClick}>Load Data</button> {isPending ? <p>Loading...</p> : <Data resource={resource} />} </> ); }

In the example above, startTransition is a function that you can use to start a transition. The isPending is a boolean that is true while the transition is pending, and false once it is complete.

Suspense for Data Fetching

Concurrent Mode also introduces the Suspense component for data fetching. This allows you to "suspend" the rendering of your component until some condition is met, such as data being fetched from an API.

import React, { Suspense } from 'react'; import Data from './Data'; function App() { return ( <Suspense fallback={<h1>Loading data...</h1>}> <Data /> </Suspense> ); }

In this example, the Data component will render once the data is fetched, and the fallback prop will be displayed while the data is being fetched.

Conclusion

Concurrent Mode is a powerful feature in React.js, providing developers with the tools to create more efficient, responsive applications. By enabling concurrent rendering, utilizing the useTransition hook, and leveraging the Suspense component for data fetching, you can enhance your application and deliver a superior user experience. As with any feature, it's essential to understand how it works to use it effectively, and we hope this guide has provided you with a solid foundation for getting started with Concurrent Mode in React.js.

Happy Coding!