5

I'm a little uncertain as how to achieve dynamic heights of a List using react-virtualized.

I have a component as follows:

import { List } from 'react-virtualized';
<List
    height={400}
    rowCount={_.size(messages)}
    rowHeight={(index) => {
        return 100; // This needs to measure the dom.
    }}
    rowRenderer={({ key, index, style }) => <Message style={style} {...messages[index]} />}}
    width={300}
/>

I have looked at using CellMeasurer as per the docs which says it can be used with the List component but I have no idea how this example actually works...

I've also tried to work out how it has been achieved in the demo code but have also reached a dead end.

Can someone please assist me on how I would measure the DOM to get each items height dynamically.

Matt Derrick
  • 5,674
  • 2
  • 36
  • 52

1 Answers1

8

Sorry you found the docs to be confusing. I will try to update them to be clearer. Hopefully this will help:

import { CellMeasurer, List } from 'react-virtualized';

function renderList (listProps) {
  return (
    <CellMeasurer
      cellRenderer={
        // CellMeasurer expects to work with a Grid
        // But your rowRenderer was written for a List
        // The only difference is the named parameter they
        // So map the Grid params (eg rowIndex) to List params (eg index)
        ({ rowIndex, ...rest }) => listProps.cellRenderer({ index: rowIndex, ...rest })
      }
      columnCount={1}
      rowCount={listProps.rowCount}
      width={listProps.width}
    >
      {({ getRowHeight, setRef }) => (
        <List
          {...listProps}
          ref={setRef}
          rowHeight={getRowHeight}
        />
      )}
    </CellMeasurer>
  )
}

There are also demos of this component here showing how it's used.

bvaughn
  • 13,300
  • 45
  • 46
  • 2
    Docs have been updated! https://github.com/bvaughn/react-virtualized/blob/master/docs/CellMeasurer.md#using-cellmeasurer-with-list – bvaughn Sep 20 '16 at 19:36
  • I'm sure most people understood the docs, I just think it was me, being me :) I will give this a try! – Matt Derrick Sep 21 '16 at 08:24
  • One more thing that would help a long way. Can you define what is listProps so I know what is being spread over . The bit that confuses me is where you map using `listProps.cellRenderer` but I'm not sure where you get `rowRenderer` from for the list. Does this mean `listProps` has both a cell and row renderer method? – Matt Derrick Sep 21 '16 at 08:41
  • Ah, I just wrote `listProps` so I could hide the unimportant details. That object contains all of the required List properties (eg rowCount, rowHeight, rowRenderer, width, and height) as well as any of the optional properties you might want to use as well. (See the full list here https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md#prop-types) – bvaughn Sep 21 '16 at 16:38
  • I wonder what that height prop is? Isn't that exactly what we want to measure? – ooxio Nov 24 '16 at 14:10
  • `CellMeasurer` docs to the rescue: https://github.com/bvaughn/react-virtualized/blob/master/docs/CellMeasurer.md. You can pass through `width` or `height` if either dimension is fixed and you only want to measure the other. Leave both out if you want to measure both. You are correct though that in this case, I prob should have been passing `width` to the `CellMeasurer` instead. I'll edit. – bvaughn Nov 25 '16 at 19:07
  • @brianvaughn Could you please add an example of calling the `renderList` function? – Poliakoff Feb 27 '17 at 23:40
  • @Polyakoff It's just a regular render function. I wrote my example this way so I didn't have to enumerate all possible `List` properties and could focus on the important bits. There are more complete examples on the react-virtualized docs, eg https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md#examples – bvaughn Feb 28 '17 at 16:59