Logo

dev-resources.site

for different kinds of informations.

Enhancing React Performance with Concurrent Rendering

Published at
12/19/2024
Categories
react
concurrentrendering
webperf
abhay
Author
abhay_yt_52a8e72b213be229
Author
25 person written this
abhay_yt_52a8e72b213be229
open
Enhancing React Performance with Concurrent Rendering

Concurrent Rendering in React

Concurrent Rendering is a React feature designed to improve the responsiveness of applications by allowing React to interrupt rendering work and prioritize more urgent updates, such as user interactions or animations. It enables smoother user experiences by breaking rendering work into smaller units and managing them efficiently.


Key Concepts of Concurrent Rendering

  1. Interruptible Rendering

    React can pause rendering tasks, work on higher-priority tasks, and resume rendering later without losing progress.

  2. Time-Slicing

    Tasks are split into small chunks, allowing React to yield control to the browser between chunks. This prevents the app from becoming unresponsive during heavy computations.

  3. Prioritized Updates

    React assigns priorities to updates, ensuring that urgent tasks like responding to user input are handled before non-urgent ones like background data fetching.


Benefits of Concurrent Rendering

  1. Improved User Experience: Applications remain responsive even during heavy rendering.
  2. Smooth Animations and Interactions: Rendering is aligned with user interactions, ensuring minimal lag.
  3. Efficient Resource Utilization: React manages rendering workload based on device capabilities.

Example of Concurrent Rendering

With React’s Concurrent Features, developers can use Transitions to manage updates.

Transitions Example

import React, { useState, useTransition } from "react";

function App() {
  const [query, setQuery] = useState("");
  const [list, setList] = useState([]);
  const [isPending, startTransition] = useTransition();

  const handleChange = (e) => {
    const value = e.target.value;
    setQuery(value);

    startTransition(() => {
      const filteredList = Array(10000)
        .fill(0)
        .map((_, i) => `Item ${i}`)
        .filter((item) => item.includes(value));
      setList(filteredList);
    });
  };

  return (
    <div>
      <input type="text" value={query} onChange={handleChange} />
      {isPending && <p>Loading...</p>}
      <ul>
        {list.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Key Points in the Code

  1. useTransition

    • Separates urgent updates (like input changes) from non-urgent updates (like filtering a large list).
    • startTransition schedules the non-urgent task.
    • isPending indicates if the non-urgent task is still in progress.
  2. Prioritization

    • React prioritizes updating the input field while processing the filtered list in the background.

React Features Leveraging Concurrent Rendering

  1. Suspense
    • Enables declarative loading states for components.
    • Works seamlessly with Concurrent Rendering to defer non-essential rendering.
   const LazyComponent = React.lazy(() => import("./LazyComponent"));

   function App() {
     return (
       <React.Suspense fallback={<div>Loading...</div>}>
         <LazyComponent />
       </React.Suspense>
     );
   }
Enter fullscreen mode Exit fullscreen mode
  1. Automatic Batching
    • Multiple state updates within an event handler are batched together, even in async code.
   function App() {
     const [count, setCount] = useState(0);
     const [text, setText] = useState("");

     const handleClick = async () => {
       setCount((c) => c + 1);
       setText("Updated"); // Both updates are batched
     };

     return <button onClick={handleClick}>Update</button>;
   }
Enter fullscreen mode Exit fullscreen mode

Enabling Concurrent Features

Concurrent Rendering features are automatically available in React 18 and above. To enable advanced features like Transitions, ensure your project is updated to the latest React version.


Use Cases for Concurrent Rendering

  1. Search and Filtering Separates urgent updates (typing in a search box) from rendering large filtered lists.
  2. Animations Ensures smooth animations by pausing non-urgent rendering tasks.
  3. Data Fetching Works with Suspense to defer rendering until data is ready.

Best Practices

  1. Use Transitions for managing updates that don’t need immediate rendering.
  2. Avoid Overusing Concurrent Features: Use them only when performance issues are evident.
  3. Test Responsiveness: Simulate different device capabilities to ensure the app remains responsive.

Limitations

  1. Not a Silver Bullet: Requires careful implementation to achieve desired performance improvements.
  2. Browser Dependency: Relies on modern browser APIs for time-slicing.
  3. Learning Curve: Developers need to understand new hooks like useTransition and concepts like Suspense.

Conclusion

Concurrent Rendering is a game-changer for React applications, offering a more responsive user experience by prioritizing tasks effectively. With features like useTransition and Suspense, React developers can build highly interactive, efficient, and smooth applications.


webperf Article's
30 articles in total
Favicon
Redefining Web Performance Standards with INP
Favicon
How to avoid frontend tech making us resentful
Favicon
Understanding PHP-FPM: Key Differences from Traditional PHP Processes and Benefits
Favicon
How Mentimeter deliver reliable live experiences at scale
Favicon
The Art of Prefetching and Preloading: Enhancing Web Performance
Favicon
The curious case of the paragraph with the bad CLS
Favicon
JavaScript Frameworks - Heading into 2025
Favicon
Technical SEO for Developers: Mastering Site Structure and Performance
Favicon
Extending Lighthouse for custom image and video optimization analysis
Favicon
Assassin ⚡️ - An open source, free database for killing slow webpages
Favicon
A Comprehensive Guide to Web Vitals: Metrics That Matter for Performance
Favicon
Why should you care about website performance?
Favicon
Screener.in Search API: A Performance Checkup! 🔎
Favicon
How Sportsbet handles 4.5M daily chat messages on its 'Bet With Mates' platform
Favicon
Enhancing React Performance with Concurrent Rendering
Favicon
Optimizing React Performance: Avoiding Unnecessary Re-renders
Favicon
Master React Profiler: Optimize Your App's Performance
Favicon
Lightweight, Transparent, Animated: Get All of Them by WebP Format
Favicon
Prerender Pages in Browser For Faster Page Load
Favicon
How we optimized perceived performance to improve our KPIs: a Hotjar case study
Favicon
Sites speed optimisation is a destination, not a journey
Favicon
How to Build a High-Performance WordPress Website: A Developer’s Guide
Favicon
Performance Optimization in React
Favicon
Subsequent Page Load Optimization 🚀
Favicon
How to Build Blazing Fast Websites with Any Framework
Favicon
Everything to know about Mobile App Performance Test Tools, Metrics, & Techniques
Favicon
Efficient State Management in Next.js: Best Practices for Scalable Applications
Favicon
Top 5 Tips to Supercharge Your Express.js App for Lightning-Fast Performance
Favicon
A useState performance tip you may not have known
Favicon
🚀 V8 Engine Secrets How We Slashed Memory Usage by 66% with TypedArrays

Featured ones: