[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;
时间: 2024-12-17 14:58:32

[React] Create a Virtualized List with Auto Sizing Cells using react-virtualized and CellMeasurer的相关文章

可变cell,自适应cell,理解iOS 8中的Self Sizing Cells和Dynamic Type

在iOS 8中,苹果引入了UITableView的一项新功能--Self Sizing Cells,对于不少开发者来说这是新SDK中一项非常有用的新功能.在iOS 8之前,如果想在表视图中展示可变高度的动态内容时,你需要手动计算行高,而Self Sizing Cells为展示动态内容提供了一个解决方案.以下是你使用Self Sizing Cells时需要注意的事项: 1.为原型单元格定义Auto Layout约束 2.指定表视图的estimatedRowHeight 3.将表视图的rowHeig

理解iOS 8中的Self Sizing Cells和Dynamic Type

本文转载至 http://www.cocoachina.com/ios/20140922/9717.html iOS开发Dynamic TypeSelf Sizing 在iOS 8中,苹果引入了UITableView的一项新功能--Self Sizing Cells,对于不少开发者来说这是新SDK中一项非常有用的新功能.在iOS 8之前,如果想在表视图中展示可变高度的动态内容时,你需要手动计算行高,而Self Sizing Cells为展示动态内容提供了一个解决方案.以下是你使用Self Siz

编译依赖于React Native0.46.4的React Native IOS工程时,出现错误“fatal error: &#39;React/RCTEventEmitter.h&#39; file not found”

我的环境: WebStorm 2017.2Build #WS-172.3317.70, built on July 14, 2017 JRE: 1.8.0_152-release-915-b5 x86_64JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.12.6 Xcode8.3.3(8E3004b) 网上搜索,可以解决我的问题的网址:https://stackoverflow.com/questions/41663002

[React] 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 win

[React] Create component variations in React with styled-components and &quot;extend&quot;

In this lesson, we extend the styles of a base button component to create multiple variations of buttons, using "extend". We can then modify the base styles in one place, and have all button types updated. import React from "react"; im

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

In this lesson, we'll use next to create a universal React application with no configuration. We'll create page components that will render on the server if accessed directly, but function as you would expect in the client. We'll use the routing capa

Html - TextArea - auto sizing to avoid scrollbar

A list of html TextArea element with class name "noteItem", below code will auto resize the TextArea to avoid scrollbar JQuery: $('.noteItem').each(function () { var scrollHeight = $(this).prop('scrollHeight'); $(this).css('height','auto'); $(th

深入React事件系统(React点击空白部分隐藏弹出层;React阻止事件冒泡失效)

只关注括号内问题的同学,可直接跳转到蓝字部分.(标题起的有点大,其实只讨论一个问题) 两个在React组件上绑定的事件,产生冲突后,使用e.stopPropagation(),阻止冒泡,即可防止事件冲突,毫无问题. 今天是踩了个React事件的坑,需求可以简化为:点击框体以外的部分则隐藏框体.最直接的想法,document上绑定个事件,设置控制显示隐藏的state为false,在框体上绑定个事件,阻止冒泡.这样点击框体内部就不会触发document上的事件. 等写完了,发现一个问题,无法阻止冒泡

[React] Use the URL as the source of truth in React

In Single Page Apps we're used to fetch the data on event callbacks. That disables the capacity to use the URL to share it to someone else and get to the same state of the app, just like in non Single Page Apps. This lesson shows you how to use React