<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shivam</title>
    <description>The latest articles on DEV Community by Shivam (@devbyshivam).</description>
    <link>https://dev.to/devbyshivam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1297050%2F09c20f3a-25d6-4fe4-92b8-81d44e588513.png</url>
      <title>DEV Community: Shivam</title>
      <link>https://dev.to/devbyshivam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devbyshivam"/>
    <language>en</language>
    <item>
      <title>Boosting Performance with List Virtualisation in React</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 23 Feb 2024 18:57:04 +0000</pubDate>
      <link>https://dev.to/devbyshivam/boosting-performance-with-list-virtualisation-in-react-5hfp</link>
      <guid>https://dev.to/devbyshivam/boosting-performance-with-list-virtualisation-in-react-5hfp</guid>
      <description>&lt;p&gt;Nowadays, every web app requires capabilities such as endless scrolling to improve the user experience. I recently encountered the same issue at work, which requires comparable capabilities when a list has hundreds or thousands of items. I began looking for a suitable solution and came upon something called the &lt;code&gt;IntersectionObserver&lt;/code&gt; API. Let's look at it in depth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Rendering large lists in React applications can significantly impact performance, especially when dealing with hundreds or thousands of items. This blog post dives into list virtualization, a technique to optimize rendering and improve user experience for extensive lists. We'll explore how to implement list virtualization in React using the &lt;code&gt;IntersectionObserver&lt;/code&gt; API.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is List Virtualization?
&lt;/h2&gt;

&lt;p&gt;List virtualization is an optimization technique that renders only the visible items in a list at any given time. As the user scrolls, additional items are rendered on-demand, creating the illusion of a continuous list while minimizing the actual number of rendered elements. This significantly improves performance and responsiveness, especially for long lists.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter &lt;code&gt;IntersectionObserver&lt;/code&gt;:
&lt;/h2&gt;

&lt;p&gt;The IntersectionObserver API provides a mechanism to monitor the intersection of an element with its ancestor or the viewport. We can leverage this API to detect when list items enter or leave the visible area and trigger rendering accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the &lt;code&gt;IntersectionObserver&lt;/code&gt; API works?
&lt;/h2&gt;

&lt;p&gt;The Intersection Observer API allows code to register a callback function when an element enters or exits an intersection with another element or the viewport.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to implement it React?
&lt;/h2&gt;

&lt;p&gt;Let's build a simple example of a virtualized list in React using &lt;code&gt;IntersectionObserver&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting Up the Project:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Start by creating a new React project using your preferred method (e.g., Create React App).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating the List Component:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useRef, useState, useEffect } from 'react';

const VirtualizedList = ({ items }) =&amp;gt; {
  const [visibleItems, setVisibleItems] = useState([]);
  const listRef = useRef(null);

  const handleIntersect = (entries) =&amp;gt; {
    const newVisibleItems = entries.filter((entry) =&amp;gt; entry.isIntersecting).map((entry) =&amp;gt; entry.target);
    setVisibleItems([...visibleItems, ...newVisibleItems]);
  };

  useEffect(() =&amp;gt; {
    const observer = new IntersectionObserver(handleIntersect);
    observer.observe(listRef.current);

    return () =&amp;gt; observer.disconnect();
  }, []);

  return (
    &amp;lt;div ref={listRef}&amp;gt;
      {visibleItems.map((item) =&amp;gt; (
        &amp;lt;div key={item.id}&amp;gt;{item.content}&amp;lt;/div&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
};

export default VirtualizedList;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;This component takes an &lt;code&gt;items&lt;/code&gt; prop containing an array of data objects.&lt;br&gt;
The &lt;code&gt;visibleItems&lt;/code&gt; state stores the currently visible items in the list.&lt;br&gt;
The &lt;code&gt;listRef&lt;/code&gt; ref holds a reference to the list container element.&lt;br&gt;
The &lt;code&gt;handleIntersect&lt;/code&gt; function handles the IntersectionObserver callback, updating the &lt;code&gt;visibleItems&lt;/code&gt; state based on the intersecting elements.&lt;br&gt;
The &lt;code&gt;useEffect&lt;/code&gt; hook sets up the IntersectionObserver, observing the list container for visibility changes.&lt;br&gt;
The component renders only the &lt;code&gt;visibleItems&lt;/code&gt; within the list container.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the VirtualizedList:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import VirtualizedList from './VirtualizedList';

const items = Array(1000).fill(null).map((_, index) =&amp;gt; ({
  id: index,
  content: `Item ${index + 1}`,
}));

function App() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Virtualized List&amp;lt;/h1&amp;gt;
      &amp;lt;VirtualizedList items={items} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;This code creates a list of 1000 items and renders only the visible ones using the &lt;code&gt;VirtualizedList&lt;/code&gt; component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits and Considerations:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Performance:&lt;/strong&gt; List virtualization significantly reduces the number of rendered elements, leading to smoother scrolling and faster UI responsiveness.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory Efficiency:&lt;/strong&gt; By rendering only visible items, memory usage is optimized, especially for large datasets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Customization:&lt;/strong&gt; You can customize the threshold for visibility and the number of items to pre-render for optimal performance based on your specific use case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced Scalability:&lt;/strong&gt; Enables efficient handling of large datasets without performance degradation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;List virtualization with IntersectionObserver is a powerful technique for building performant and scalable React applications. By implementing this approach, you can ensure a smooth user experience even when dealing with large datasets. Remember to tailor the implementation to your specific needs and continuously improve your skills in building efficient and user-friendly applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Exploration:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Explore libraries like react-window or react-virtualized for more advanced list virtualization functionalities.&lt;/li&gt;
&lt;li&gt;Experiment with different optimization techniques beyond list virtualization to create even more performant React applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this blog helps you get started with list virtualization in React! &lt;br&gt;
If you have any questions or recommendations, please share them in the comments section.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
