码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-06-27 08:12:07      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:measure   fixed   width   render   erer   inf   using   scroll   ntc   

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

标签:measure   fixed   width   render   erer   inf   using   scroll   ntc   

原文地址:http://www.cnblogs.com/Answer1215/p/7083156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!