[React] Create a Virtualized List with Auto Sizing Cells using react-virtualized and CellMeasurer(代码

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React] Create a Virtualized List with Auto Sizing Cells using react-virtualized and CellMeasurer(代码相关的知识,希望对你有一定的参考价值。

In this lesson we‘ll use CellMeasurer and CellMeasurerCache to automatically calculate and cache the height of a row. This will allow us to remove the predefined rowHeight on list and allow for dynamically sized rows.

 

import React, {Component} from ‘react‘;
import {AutoSizer, List, CellMeasurer, CellMeasurerCache} from ‘react-virtualized‘;

const ScreenInfo = ({width, height}) => (<span>width: {width} height: {height}</span>);

class App extends Component {

    constructor(props) {
        super(props);
        this.cache = new CellMeasurerCache({
            fixedWidth: true,
            defaultHeight: 50
        });
    }
    renderRow = ({key, isScrolling, parent, style, index}) => {
        return (
        <CellMeasurer
            key={key}
            cache={this.cache}
            parent={parent}
            columnIndex={0}
            rowIndex={index}
        >
            <div style={style} >
                name: {this.props.data[index].name}
                email: {this.props.data[index].email}
                height: <div style={{height: `${this.props.data[index].randomHeight}px`}}>{this.props.data[index].randomHeight}px</div>
            </div>
        </CellMeasurer>
        );
    };

    render() {
        return (
            <AutoSizer>
                {({width, height}) => {
                    return (
                        <div>
                            <ScreenInfo width={width} height={height}/>
                            <List
                                rowCount={this.props.data.length}
                                deferredMeasurementCache={this.cache}
                                rowHeight={this.cache.rowHeight}
                                rowRenderer={this.renderRow}
                                width={width}
                                height={height}
                            />

                        </div>
                    );
                }}
            </AutoSizer>
        );
    }
}

export default App;

 

以上是关于[React] Create a Virtualized List with Auto Sizing Cells using react-virtualized and CellMeasurer(代码的主要内容,如果未能解决你的问题,请参考以下文章

不想eject,还咋修改create-react-app的配置?

Create React App 入门及 webpack 配置

[React] Create & Deploy a Universal React App using Zeit Next

扩展 Create React App 的 Webpack 配置

构建react项目,The react-scripts package provided by Create React App requires a dependency

Create a Draggable Opacity Changing Circle with Reanimated in React Native