React Virtualization

Rendering large lists in React can lead to performance issues due to large number of DOM elements being created and managed. React Virtualization is a technique used to optimize the rendering of large lists by only rendering the items that are currently visible on the screen. This significantly reduces the memory footprint and improves the performance of your application.

It is not a part of core React, So you have to install it separately by using the following command.

npm install react-virtualized

Example: Below is the code example:

JavaScript
// index.js 

import React,
{
    useState,
    useEffect
} from 'react';
import { List as VirtualList } from 'react-virtualized';
const LARGE_LIST_SIZE = 1000; // Number of items in the list

function VirtualListComp() {
    const [items, setItems] = useState([]);

    // Simulate data generation (replace with your actual data fetching logic)
    useEffect(() => {
        const newItems = [];
        for (let i = 1; i <= LARGE_LIST_SIZE; i++) {
            newItems.push({ id: i, content: `Item ${i}` });
        }
        setItems(newItems);
    }, []);

    const rowRenderer = ({ index, style }) => {
        const item = items[index];
        return (
            <div key={item.id} style={style}>
                {item.content}
            </div>
        );
    };

    return (
        <div className="App">
            <h1>Large List Example</h1>
            <VirtualList
                width={400}
                height={400}
                rowCount={items.length}
                rowHeight={40} // Height of each item
                rowRenderer={rowRenderer}
            />
        </div>
    );
}

const root = ReactDOM.createRoot(
    document.getElementById('root')
);
root.render(
    <div id='main_container'>
        <div>
            <img src=
'https://media.w3wiki.org/gfg-gg-logo.svg' alt='gfg_logo' />
        </div>
        <VirtualListComp />
    </div>
);

Output:

React Virtualization Output

Mastering Performance Optimization Techniques with React Hooks

In this article, we will explore advanced performance optimization techniques using React Hooks. We’ll delve into memoization, callback optimization, preventing unnecessary renders, state updates, and more. By mastering these techniques, developers can significantly enhance the performance of their React applications.

Similar Reads

1. UseMemo:

The useMemo hook in React.js allows you to memorize the result of a function, meaning it caches the output and only re-evaluates the function when its dependencies change. This can improve performance by avoiding unnecessary computations on every render....

2. useCallback

React’s useCallback hook is another tool for performance optimization. Unlike useMemo which caches the result of a function, useCallback focuses on memorizing the function itself. UseCallback allows you to memorize a function, meaning it returns the same function reference as long as its dependencies haven’t changed. Here’s how it helps....

3. React.memo

In React, React.memo is a Higher-Order Component (HOC) that helps improve performance by preventing unnecessary re-renders of functional components. It use Memoization technique to optimize the performance of React components by caching the result of the component’s rendering and reusing it if the component’s props remain unchanged. It leads to lower memory usage due to less re-render. Here’s a breakdown of how it works:...

4. UseEffect:

UseEffect is a powerful hook in React for performing side effects like data fetching, subscriptions, or setting up timers. By specifying dependencies, you can control when the effect runs. If any of the dependencies change between renders, the effect function will be called again and if dependencies array is empty it runs only once. However, if used incorrectly, it can lead to unnecessary re-renders, impacting your application’s performance...

5. React Virtualization

Rendering large lists in React can lead to performance issues due to large number of DOM elements being created and managed. React Virtualization is a technique used to optimize the rendering of large lists by only rendering the items that are currently visible on the screen. This significantly reduces the memory footprint and improves the performance of your application....

6. useRef:

UseRef() is a hook provided by React that creates a mutable reference object which persists across renders of a functional component. It returns a single mutable value object ({current}) that can be updated without causing re-renders. useRef() is commonly used to access or store values that persist between renders without triggering component re-renders....