Virtualizing Large Data Lists with react-window


Rendering large dataset in the DOM without the right tools can be expensive as it has some adverse effect on the web page performance; it slows down runtime, network requests, memory performance. In this tutorial, we’ll cover on how render large amount of data, either from an external API or a dummy data file within our application. We’ll use a React library, react-window, to render this large amount of data. Then we’ll build a simple e-commerce app displaying a list of products.

This article will help web developers to build a better web application that displays and handles large amount of data in either a list format or grid format. It will introduce and explore solutions to the shortcomings with infinite scroll or pagination provided in virtualization of data. It will also cover the best UI/UX practices needed when building applications that render large data within it.

What is virtualization?

Virtualization as a scrolling technique is the rendering process that keeps track of the scrolling position of a user, and visually displays only the DOM contents within that current viewport’s position. This technique simply involves handling large dataset in an infinite scroll pattern but incrementally load and render the long lists of data just as the data enters the viewport. This concept presents developers with all the performance advantages of pagination while at the same time providing the UX benefits gained from infinite scrolling.

Using virtualization is a common solution used in rendering large amount of posts in social media applications, delivering large news data in news media applications, and also in bookkeeping (Accounting) software. The endless two dimensional tile scrolling in Map applications also involves virtual scrolling which aids easy display of the constant changing locations of the users when using it. And virtualization supports displaying these large datasets in grid format or list (spreadsheets-like) format.

A simple implementation of virtualization involves a pre-calculation of the aggregate height of the list of data using the Intersection Observer API provided by the Web API and multiplying it with total count of the list items. But for this tutorial, we’ll use another easier approach, by using react-window.

React-window is a simplified version of react-virtualized package for easily implementing virtualization in React applications through the APIs (components) provided by the library. According to the docs, React window works by only rendering part of a large dataset (just enough to fill the viewport) which reduces the amount of work (and time) required to render the initial view and to process incoming updates to the UI, and also reduces the memory footprint by avoiding over-allocation of DOM nodes.

The Four react-window Components

Let’s go through the four available components that can be used to render lists of items when using react-window within React applications.

  • FixedSizeList — This component helps render elements of fixed size defined through the index prop. Common available props that can be passed into this component are: useIsScrolling, itemCount , itemSize, itemData, innerElementType, etc
  • VariableSizeList — This component accepts the same props as the FixedSizeList component, but with additional props like an estimatedItemSize, and itemSize which takes in a function in this case and returns the size of a item in the direction being windowed
  • FixedSizeGrid — With this component, we’re rendering using dimensions: vertical (columns) and horizontal (rows) directions. Another difference is that we’ll have to add a data’s count. Common props are rowCount, rowWidth, columnCount, columnWidth, height, width e.t.c
  • VariableSizeGrid — This component is as FixedSizeGrid, with additional props: columnWidth & rowHeight which accepts functions as their prop values.

For this tutorial, we’ll focus on using the FixedSizeList component, which gives a beginner-friendly introduction to using react-window within our React applications (a simple e-commerce application displaying a list of products.).

Implementing react-window In React Apps

Now let’s install react-window using yarn in a React application. In this tutorial, react-window will be installed as a dependency, while the types for it will be installed as a devDependency. We’ll also install faker.js as dependency:

1yarn add react-window faker

2yarn add -D @types/react-window

After the installation has successfully been completed, go ahead to open the ./src/App.js file and include the following JSX markup:

1import { useState } from "react";

2import * as faker from "faker";

3

4const App = () => {

5 const [data, setData] = useState(() =>

6 Array.from({ length: 1200 }, faker.commerce.product)

7 );

8

9 return (

10 <main>

11 <ul style={{ width: "400px", height: "700px", overflowY: "scroll" }}>

12 {data.map((product, i) => (

13 <li key={i + product}>{product}</li>

14 ))}

15 </ul>

16 </main>

17 );

18};

In the above code, we defined the structural markup for the list of data being rendered into the DOM. Then passed a basic style prop to the ul tag to add a fixed width and height styles, also with an overflow set to scroll on it. Also we’ll use the React hook, useState, to mange our data state provided by faker.js’s dummy data. We create an array of size 1200 from it, and populate the array with enough data from faker.js.

null

Next, we’ll start implementing virtualization using the react-window’s component FixedSizeList. This component is responsible for rendering an individual item of a fixed size specified by the index prop within the list:

1import { useState } from "react";

2import * as faker from "faker";

3import { FixedSizeList as List } from "react-window";

4

5const App = () => {

6 const [data, setData] = useState(() =>

7 Array.from({ length: 1200 }, faker.commerce.product)

8 );

9

10 return (

11 <main>

12 <List

13 innerElementType="ul"

14 itemData={data}

15 itemCount={data.length}

16 itemSize={20}

17 height={700}

18 width={400}

19 >

20 {({data, index, style }) => {

21 return (

22 <li style={style}>

23 {data[index]}

24 </li>

25 );

26 }}

27 </List>

28 </main>

29 );

30};

The List element uses two markup approaches defined by it’s props, the innerElementType and outerElementType props. Their default values are divs but in our use case, we’ll set the outerElementType to ul. Then we’ll pass in li as the children prop to the List component, providing it with a necessary styles parameters (absolute positioning styles, etc.). Also providing a width and height props to pass the previous styles we earlier defined through the style prop. Then using the itemCount prop, we are creating an imaginary array with the same length of our state, data. Then we explicitly make this data available to the li elements through the itemData prop.

Let’s modify the unique value for the key prop on the li elements by the using the itemKey prop made available on the List component. We’ll use the faker.datatype.uuid function to generate random UUID and populate the itemKey prop with it.

1...

2const App = () => {

3 ...

4 return (

5 <main>

6 <List

7 innerElementType="ul"

8 itemData={data}

9 itemCount={data.length}

10 itemKey={faker.datatype.uuid}

11 itemSize={20}

12 height={700}

13 width={400}

14 >

15 {({data, index, style }) => {

16 return (

17 <li style={style}>

18 {data[index]}

19 </li>

20 );

21 }}

22 </List>

23 </main>

24 );

25};

This code will make sure we’re using React’s lists and key best practices as defined by the docs. The term “key” is used to describe a special string attribute that needs to be included when creating lists of elements in React applications.

With react-window, we can go further to define more structured UI elements within the children prop passed into the List component without tampering with the optimised performance.

1...

2const App = () => {

3 const [data, setData] = useState(() =>

4 Array.from({ length: 1200 }, () => ({

5 productImg: faker.image.business(200, 600, true),

6 product: faker.commerce.product(),

7 productPrice: faker.commerce.price(2, 22, 1, "$"),

8 productDescription: faker.commerce.productAdjective(),

9 }))

10 ); return (

11 <main>

12 <List

13 innerElementType="ul"

14 itemData={data}

15 itemCount={data.length}

16 itemKey={faker.datatype.uuid}

17 itemSize={20}

18 height={700}

19 width={400}

20 >

21 {({data, index, style }) => {

22 return (

23 <li className="py-4 border-2 border-indigo-400 flex" {...props}>

24 <img className="h-10 w-10 rounded-full" src={productImg} alt="" />

25 <div className="ml-3">

26 <p className="space-x-1">

27 <span className="text-base text-gray-900 whitespace-nowrap">

28 {product}

29 </span>

30 <span

31 className="text-gray-600 text-base font-extrabold"

32 style={{

33 backgroundImage:

34 "linear-gradient(transparent 50%, pink 50%, pink 85%,transparent 85%,transparent 100%)",

35 }}

36 >

37 {productPrice}

38 </span>

39 </p>

40 <p className="text-sm text-gray-500">{productDescription}</p>

41 </div>

42 </li>

43 );

44 }}

45 </List>

46 </main>

47 );

48};

Here, we updated the state of our application with more data fields to help model an online shop that has the following attributes: image (productImg), name (product), price (productPrice) and description (productDescription) of its products. We also defined more elements to hold this additional data.

(Note: To replicate the UI styles used in this tutorial, make sure to include the below tailwind.css CDN to the /public/index.html file.)

1<link

2 href="https://unpkg.com/[email protected]^2/dist/tailwind.min.css"

3 rel="stylesheet"

4 />

null

If you inspect the example List component using browser Devtools, you’ll notice that no matter how lengthy we scroll down the long list of the items within our application, the number of items in its parent div never changes. What does change is how far each one is absolutely positioned from the top. Thus, making sure we see only the UI elements for the items in the viewport.

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue.
It’s like having your browser’s inspector open while looking over your user’s shoulder.
OpenReplay is the only open-source alternative currently available.

OpenReplay

Happy debugging, for modern frontend teams – Start monitoring your web app for free.

Conclusion

In this tutorial, we’ve discussed on how to leverage virtualization to optimize the DOM when rending large amount of dataset into it. Then introduced react-window as a React package that efficiently implements virtaulization behind the hood, to render a large dataset and at the same time speeds up performance targets within our application. It does this by rendering only the data that are within the viewport of the browser. Get the code used in this tutorial on GitHub.

Resources



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here