<?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: Dip Chowdhury</title>
    <description>The latest articles on DEV Community by Dip Chowdhury (@dipbd1).</description>
    <link>https://dev.to/dipbd1</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%2F899874%2Fdc6fa651-d62c-41f1-b5b4-14638365b6ca.png</url>
      <title>DEV Community: Dip Chowdhury</title>
      <link>https://dev.to/dipbd1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dipbd1"/>
    <language>en</language>
    <item>
      <title>Virtualize API call for loading Large Data in React-Select with React-Window</title>
      <dc:creator>Dip Chowdhury</dc:creator>
      <pubDate>Wed, 20 Sep 2023 10:25:39 +0000</pubDate>
      <link>https://dev.to/dipbd1/virtualize-api-call-for-loading-large-data-in-react-select-with-react-window-10gj</link>
      <guid>https://dev.to/dipbd1/virtualize-api-call-for-loading-large-data-in-react-select-with-react-window-10gj</guid>
      <description>&lt;p&gt;Hello internet folks! I’m pretty new to writing and sharing stuff on the internet, but I believe that by sharing our knowledge, we can build new cybernetic pyramids full of computational power! 🧑🏾‍💻🧑🏾‍💻🧑🏾‍💻&lt;/p&gt;

&lt;p&gt;A few days ago, I was loading some data for my current clients at “CDP” where I was trying to show 1000+ data. Everything was going well until we received a requirement to scrape and find 75000+ entries and display them using React-Select, which was previously handling the 1000+ data.&lt;/p&gt;

&lt;p&gt;Given my experience in this field, as soon as I heard the news, I knew that React wouldn’t be ideal for creating 75000+ list components to select from, especially when each of them might have their own CSS and listeners.&lt;/p&gt;

&lt;p&gt;So what can we do? In the &lt;a href="https://react-select.com/advanced"&gt;React-select Advance&lt;/a&gt; section, you might find a solution where you listen to keystrokes or the end of scrolling to bring in new data. However, I was thinking of pulling all the data back into my client and then processing it virtually using some other technique.&lt;/p&gt;

&lt;p&gt;I came across some ideas on Stack Overflow that might help you. In another post, I may explain it in more detail, but for now, you can have a look at this code. It will help you with your problem.&lt;/p&gt;

&lt;p&gt;Happy coding, and see you in the next post! 🕸🕸🕸&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`import React, { useState, useCallback, useEffect, useRef } from "react";
import { FixedSizeList as List } from "react-window";
import InfiniteLoader from "react-window-infinite-loader";

const LOADING = 1;
const LOADED = 2;

const CustomMenuList = ({ children, initialOffset }) =&amp;gt; {
    const itemHeight = 50;
    const [itemStatusMap, setItemStatusMap] = useState({});

    const itemStatusMapRef = useRef(itemStatusMap);
    useEffect(() =&amp;gt; {
        itemStatusMapRef.current = itemStatusMap;
    }, [itemStatusMap]);

    const isItemLoaded = useCallback((index) =&amp;gt; !!itemStatusMapRef.current[index], []);

    const loadMoreItems = useCallback((startIndex, stopIndex) =&amp;gt; {
        setItemStatusMap((prevStatusMap) =&amp;gt; {
            const newStatusMap = { ...prevStatusMap };
            for (let index = startIndex; index &amp;lt;= stopIndex; index++) {
                newStatusMap[index] = LOADING;
            }
            return newStatusMap;
        });

        return new Promise((resolve) =&amp;gt; {
            setTimeout(() =&amp;gt; {
                setItemStatusMap((prevStatusMap) =&amp;gt; {
                    const newStatusMap = { ...prevStatusMap };
                    for (let index = startIndex; index &amp;lt;= stopIndex; index++) {
                        newStatusMap[index] = LOADED;
                    }
                    return newStatusMap;
                });
                resolve();
            }, 2500);
        });
    }, []);

    const Row = React.memo(({ index, style }) =&amp;gt; {
        let label;
        if (itemStatusMap[index] === LOADED) {
            label = children[index];
        }
        return (
            &amp;lt;div style={{ ...style }}&amp;gt;
                {label || "Loading..."}
            &amp;lt;/div&amp;gt;
        );
    });

    return (
        &amp;lt;InfiniteLoader
            isItemLoaded={isItemLoaded}
            itemCount={children.length}
            loadMoreItems={loadMoreItems}
            threshold={5}
        &amp;gt;
            {({ onItemsRendered, ref }) =&amp;gt; (
                &amp;lt;List
                    height={500}
                    itemCount={children.length}
                    itemSize={itemHeight}
                    initialScrollOffset={initialOffset}
                    onItemsRendered={onItemsRendered}
                    ref={ref}
                    width="100%"
                &amp;gt;
                    {Row}
                &amp;lt;/List&amp;gt;
            )}
        &amp;lt;/InfiniteLoader&amp;gt;
    );
};

export default CustomMenuList;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
