简体   繁体   中英

Dynamic height with infinite scroll (react-virtualized)

I'm using 'react-virtualized' to render an infinite scrolling list.

However, I am having trouble rendering the rowHeight dynamically. I made an attempt, but it only is relevent for desktop, and feels wrong.

I tried following the examples, but had no success.

What is the correct way to calculate the true row height?

It should be responsive to mobile.

Here is an example:

https://codesandbox.io/s/ADARgvlxB

class App extends React.Component {

  render() {

    return (
      <div>

        <AutoSizer>
        {({ height, width }) => (
        <List
          height={600}
          width={width}
          rowCount={foo.length}
          rowHeight={({ index }) => {
            const x = foo[index];
            if (x.name.length < 10) { return 20; } 
            else if (x.name.length > 9) { return 40;}
          }}
          rowRenderer={({ index, style }) => {
            const x = foo[index];
            return (
              <div key={index} style={style}>
                {x.name}
              </div>
            );
          }}
        />
        )}
        </AutoSizer>
      </div>
    );
  }
}

What is the correct way to calculate the true row height?

This is what the react-virtualized CellMeasurer component is for. You can see a demo of it measuring dynamic heights here . The source code for the demo are the *.example.js files here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM