logotype
  • About Us
  • Services
  • Expertise
  • Startups
  • Portfolio
  • Blog
  • Careers
  • UK
    • UK
    • AL
get in touch

Type [To] Search

Search
Close
  • About Us
  • Services
  • Expertise
  • Startups
  • Portfolio
  • Blog
  • Careers
  • UK
    • UK
    • AL
logotype

Type [To] Search

logotype
  • About Us
  • Services
  • Expertise
  • Startups
  • Portfolio
  • Blog
  • Careers
  • UK
    • UK
    • AL
Button Link Example
Top Tips

HOW TO RENDER BIG LISTS IN REACT

November 21, 2022

Working with web apps we have to render tables or lists of data often. Most times lists do not contain many records and it is fine. However problems arise when app scales and now lists have thousands of records.

To solve this problem we can implement a paradigm called Virtualization

For starters let’s create a simple component which fetches 1000 records of photos.

usePhotos.tsx

import { useEffect, useState } from 'react';
import Photo from './Photo';
 
function usePhotos() {
  const [photos, setPhotos] = useState<Photo[] | null>(null);
 
  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/photos?_limit=1000')
      .then((response) => response.json())
      .then((photosData) => {
        setPhotos(photosData);
      });
  }, []);
 
  return {
    photos,
  };
}
 
export default usePhotos;
And another component PhotosList to render each photo
import React from 'react';
import styles from './styles.module.scss';
import usePhotos from './usePhotos';
import PhotoCard from './PhotoCard/PhotoCard';
 
const PhotosList = () => {
  const { photos } = usePhotos();
 
  if (!photos) {
    return null;
  }
 
  return (
    <div className={styles.wrapper}>
      {photos.map((photo) => (
        <PhotoCard key={photo.id} photo={photo} />
      ))}
    </div>
  );
};
 
export default PhotosList;
While the above solution works, it is far from optimal.

Rendering 1000 div’s in DOM is not optimal at all.
This is where Virtualization comes in handy.

If we render a large list, the user does not see all its contents at once and uses a scrollbar. When we implement virtualization, we don’t render the elements of the list that are not currently visible. By doing that, we make the DOM tree creation a lot faster. Besides that, the browser does not need to fetch all the images simultaneously.

To implement virtualization in this article, we use the react-window library.

npm install react-window @types/react-window

PhotosList.tsx

import React from 'react';
import usePhotos from './usePhotos';
import PhotoCard from './PhotoCard/PhotoCard';
import { FixedSizeList } from 'react-window';
 
const PhotosList = () => {
  const { photos } = usePhotos();
 
  if (!photos) {
    return null;
  }
 
  return (
    <FixedSizeList height={800} width={600} itemCount={photos.length} itemSize={155}>
      {({ index, style }) => {
        const photo = photos[index];
        return <PhotoCard key={photo.id} photo={photo} style={style} />;
      }}
    </FixedSizeList>
  );
};
 
export default PhotosList;

Before & After Virtualization web site performance

Notice FixedSizeList has a defined row item width and height.

But it does not have to.

We can make it dynamic using another helper library.

npm install react-virtualized-auto-sizer @types/react-virtualized-auto-sizer

PhotosList.tsx

import React from 'react';
import usePhotos from './usePhotos';
import PhotoCard from './PhotoCard/PhotoCard';
import { FixedSizeList } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';
 
const PhotosList = () => {
  const { photos } = usePhotos();
 
  if (!photos) {
    return null;
  }
 
  return (
    <AutoSizer>
      {({ height, width }) => (
        <FixedSizeList
          height={height}
          width={width}
          itemCount={photos.length}
          itemSize={155}
        >
          {({ index, style }) => {
            const photo = photos[index];
            return <PhotoCard key={photo.id} photo={photo} style={style} />;
          }}
        </FixedSizeList>
      )}
    </AutoSizer>
  );
};
 
export default PhotosList;

We can use AutoSizer to manage only width or height instead of both. To do that, we need to use the disableHeight or disableWidth  attributes.

That’s it.

Now you can render endless lists without worrying about performance.

reactjs
TRENDS IN TECHNOLOGY CAN DEFINE THE FUTURE OF BUSINESS.

TRENDS IN TECHNOLOGY CAN DEFINE THE FUTURE OF BUSINESS.

July 13, 2022

MICROFRONTENDS WITH REACT

November 21, 2022
MICROFRONTENDS WITH REACT

Related Posts

Top Tips
April 25, 2024By natalia

5-STEP GUIDE TO E-COMMERCE EXCELLENCE

READ MORE
Top Tips
June 3, 2022By jona dajci

WHY IS OUTSOURCING IN ALBANIA A GREAT IDEA?

READ MORE

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • CANCELLED FLIGHTS – MAJOR OUTAGE DISRUPTS GLOBAL USERS
  • WHEN TO HIRE A FULL-STACK DEVELOPER vs. A DEVELOPMENT AGENCY
  • 8 MISTAKES TO AVOID WHEN OUTSOURCING SOFTWARE DEVELOPMENT
  • 5-STEP GUIDE TO E-COMMERCE EXCELLENCE
  • UNLOCKING SUCCESS: THE CRUCIAL ROLE OF MVP IN LAUNCHING YOUR BUSINESS

Recent Comments

No comments to show.

Archives

  • July 2024
  • May 2024
  • April 2024
  • November 2022
  • July 2022
  • June 2022
  • April 2022

Categories

  • Case Study
  • Software
  • Top Tips
  • Trends
logotype
We are an Innovative Software Company with a passion for excellence

Contacts

info@atrax.uk

Linkedin Instagram Facebook

Services

  • Minimum Viable Product
  • Product Development
  • Dedicated Teams
  • Staff Augmentation

Industries

  • Cyber Security
  • Hospitality & Real Estate
  • Fintech
  • Healthcare
  • eGovernment
  • © 2024 All Rights Reserved.
  • Cookie policy
  • Privacy policy
  • Terms of use
BACK TO TOP