Skip to content

Latest commit

 

History

History
137 lines (82 loc) · 8.01 KB

react-concurrent-mode.md

File metadata and controls

137 lines (82 loc) · 8.01 KB

what is react concurrent mode

React Concurrent Mode is a new feature introduced in React 18 that aims to improve the user experience by making the rendering process more efficient and responsive. Concurrent Mode is a set of features that allows React to prioritize and coordinate different tasks, such as rendering, data fetching, and event handling, in a more efficient way. This helps to improve the overall performance of the application, especially in scenarios where there are multiple updates or long-running tasks.

Here are some key aspects of React Concurrent Mode:

1- Suspense: Suspense is a feature that allows components to "suspend" their rendering until certain data or resources are available. This helps to prevent the user from seeing a blank screen or a loading spinner while the application is waiting for data to be fetched.

2- Transitions: Transitions are a way to manage state updates in a more efficient way. Instead of immediately re-rendering the entire application when a state change occurs, Transitions allow you to prioritize certain updates and delay others, ensuring a smoother user experience.

3- Selective Hydration: Selective Hydration is a technique that allows React to only hydrate the parts of the application that are visible to the user, rather than hydrating the entire application at once. This can significantly improve the initial load time of the application.

Optimizing Performance with React Concurrent Mode

React Concurrent Mode is a groundbreaking addition to the React library that enables more responsive and fluid user interfaces by allowing components to render asynchronously. Traditionally, React updates its UI in a synchronous manner, which means that when a component re-renders, it blocks the main thread until the update is complete. This synchronous rendering model can lead to janky user experiences, especially when dealing with complex UIs or slow fetching processes.

Concurrent Mode, on the other hand, introduces the concept of "time-slicing," where React can pause and resume rendering work to prioritize high-priority updates while keeping the UI responsive. This enables smoother transitions, faster loading times, and better overall performance, particularly on low-powered devices or networks.

Concurrent Mode is a feature in React that allows for more responsive user interfaces by breaking down the rendering work into smaller chunks and prioritizing which parts of the UI to render first. It was introduced in React version 16. It used in React to pause, abort, or resume rendering updates based on their priority, thus ensuring that high priority updates, e.g. user interactions or animations are processed without delay, but in case of lower priority updates, it can be deferred or interrupted to prevent blocking the main thread.

Concurrent features

  • Prioritizing Updates: Concurrent Mode allows react to prioritize updates based on their importance. It ensures that higher priority updates are processed first, lower priority updates processed only after the higher priority updates. It improves the perceived performance of the application.

  • Time slicing: Time slicing is a technique used in Concurrent Mode to break down the rendering work into smaller units called "time slices" or "tasks". These tasks are executed increamentally, allowing React to pause and resume rendering as needed without blocking the main thread.

  • Suspense: Suspense is another feature of Concurrent Mode. It allows component to suspend rendering while waiting for asynchronous data to resolve. This helps in creating smoother user experiences by avoiding empty loader states or placeholder content.

  • Error Boundaries: Concurrent Mode enhances error handling in React by introducing improvements to error boundaries. Error boundaries in Concurrent Mode can gracefully handle errors that occur during rendering, suspending, or resuming components.

  • Improved User Experience: By enabling smoother rendering and prioritizing important updates, Concurrent Mode significantly improves the perceived performance and responsiveness of React applications, especially in scenarios involving complex UIs or heavy rendering workloads.

In React, concurrent rendering revolutionizes how apps handle heavy loads and manage updates. Unlike traditional synchronous rendering, concurrent rendering allows React to multitask without blocking the main thread.

Concurrent rendering prioritizes a fluid user experience, enabling React to pause ongoing rendering to address urgent tasks, like responding to user input. This ensures immediate responses to user interactions, even during extensive rendering tasks.

some examples of usage in concurrent mode

  • Prioritizing User Input:

Concurrent mode lets React prioritize certain tasks, like handling user input. For instance, imagine a search bar filtering a large list of items as the user types. In a traditional setup, filtering might lag behind typing. But with concurrent mode, React can pause filtering to keep the input responsive.

  • Smooth Loading States with Suspense

Another cool feature of concurrent mode is Suspense. It's great for creating smooth loading states in your components.

Let's say you're fetching data from an API. Normally, managing the loading state manually can be clunky. But with Suspense, you can just tell React what to show while waiting for data.

<Suspense fallback={<div>Loading...</div>}>

  <DataFetchingComponent />

</Suspense>

Here, if DataFetchingComponent is still fetching data, React shows the fallback loading message.

React introduces a concurrent renderer that works on multiple tasks at the same time, without blocking the main thread. In concurrent mode react interrupts an ongoing task to handle a new task that is more urgent than the current task.*

import { startTransition } from "react";

function MyComponent() {
  const [state, setState] = useState(initialState);

  function handleClick() {
    startTransition(() => {
      setState(newState);
    });
  }

  // ...
}

In this example, we are using the startaTransition function to mark a state update as a transition. This is useful to tell React that this update is non-urgent and this can be interrupted if there is another urgent task.

Concurrent Rendering and Concurrent Features in React 18

You can improve your app's performance using concurrent rendering by declaring some states as non-important or we can say that non-urgent. The new concurrent features in your app will work automatically in the specific areas that use them since these features were built on top of concurrent rendering.

  • StartTransition API:

It is used to keep the app responsive without blocking user interaction by allowing us you to mark specific updates as transitions.

    import { startTransition } from 'react';
    
    // Urgent
    setInputValue(input);
    
    // Mark any state updates inside as transitions
    startTransition(() => {
      // Transition
      setSearchQuery(input);
    })
  • useTransition API: By using useTransition API React can track and update pending state transitions with the usePending flag. It helps to display loading feedback to users so that users will know that something is happening in the background.
import { useTransition } from 'react';
    
    const [isPending, startTransition] = useTransition();
    
    {isPending && <Spinner />}

useDefferedValue API :

This API helps keep the UI responsive by letting React delay updates to slow parts of the screen. For instance, if one part of the UI updates quickly and another part takes longer, React can hold off updating the slower part, showing its old value while other components update.

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