对于如何使用react-virtualized实现列表的动态高度,我有点不确定。
我有一个组件如下所示:
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}
/>我已经看过如何根据文档使用CellMeasurer,文档说它可以与List组件一起使用,但我不知道这个示例实际是如何工作的……
我也试图弄清楚它是如何在demo code中实现的,但也走到了死胡同。
谁能帮助我如何测量DOM,以获得每个项目的高度动态。
发布于 2016-09-21 03:30:52
很抱歉,您发现文档令人困惑。我会试着更新它们,让它们更清晰。希望这能有所帮助:
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>
)
}还有这个组件here的演示,展示了它是如何使用的。
https://stackoverflow.com/questions/39600808
复制相似问题