Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create an Auto Resizing Virtualized List with react-virtualized


    In this lesson we'll show how to use the AutoSizer component from react-virtualized to automatically measure the width/height of our content area. We'll then use theList component to render our set of data as a virtualized list into the DOM using windowing.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    React-virtualized allows you to render thousands, or even millions of items using a technique called windowing. This means that instead of rendering all million items into the DOM, it will only render what can fit on the screen.

    Our current application has an index that pulls in a generate data function. Our generate data function just receives a count, and loops over and uses Faker to generate some fake data.

    If we go back to our index, we default it to a thousand, but this could be anything from a thousand to a million, and react-virtualized will handle it just fine.

    One other thing to note is that in our index.html, we have a root that this is where we're rendering to, and in our index.css, we're saying that this root is 100 VH, which is a viewport pixel, which just means that it will take up the entire screen that we're looking at.

    Now let's switch back to our app, and we'll get started on adding a list. First thing we'll do is change this div to AutoSizer. We'll open it up, re-close it, and add a child as a function. This function as a child will receive width and height.

    What this will do is automatically re-render this child, which is just a function, with a new width and height whenever anything happens, AKA a resize or whatever. We can prove that by returning a span here, and we'll say width is width pixels, and then we'll just say the height is height pixels.

    We close that, and we'll go to our browser. When it refreshes, we can see that the width is rendering here and the height, and whenever we adjust anything, it will re-render. Even on the width, when we adjust that, it'll re-render.

    Rather than just returning a span here, we'll return our list. List is imported from react-virtualized here, and it takes two properties. Width, which we're getting here, width, and the height.

    We'll also add a few other pieces of information. One will be the row count. This will be equal to the amount of rows that we have, so this.props.data.length, and that just refers to the thousand items that we're generating here and passing in as our data prop.

    We'll also need to select a row height. The row height is the height of each particular row, and we'll just say it's 50 pixels, and then we'll need to add a row renderer. We'll just say this.renderRow.

    This row renderer will be the function that gets called with some pieces of information to render each particular row, so we need to set it up. Let's say renderRow is equal to a function here, and it will receive an object of various pieces of information, including the index, isScrolling, key, and style.

    The two pieces of information that are most important are the key and the style. Here we'll return a div, and we need to apply the key here, and the style. The style is an automatically generated style that will absolutely position this particular row on the screen appropriately.

    Also, to access the piece of information we'll add a div here, and then this.props.data. We have the index of the data, so we'll say index, and then .name. We'll also add the email on the screen, as well.

    The key is going to be an identifier for React to tell it that this particular component that's being returned is the exact same across scrolls. This will help with performance, and the style, we'll take a look at here in the DOM.

    You can see now we have our entire list re-rendering inside of our div, and a react-virtualized content, and then another react-virtualized div that's a calculated height, and then these are each of our divs. As you can see, our style property is applying the height of 50, left of zero, and then adjusting the tops.

    As we scroll, we can see that our styles are being changed and things are being re-rendered as we scroll. However, only a certain number are in the DOM. There's not all 1,000 being rendered.