【发布时间】:2018-05-09 09:33:20
【问题描述】:
我正在开发一个基于 react-virtualized 的数据网格组件。它应该有一个带有可调整大小列的固定标题。我希望标题根据标题单元格内容更改其高度。我正在使用CellMeasurer 来计算单元格高度并更新标题的高度。
问题是单元格大小是在渲染单元格后计算的(afaik),所以如果高度已更改,我必须在标题的render 内调用forceUpdate。
这是render 的样子(完整示例是here):
render() {
const height = this._cache.rowHeight({ index: 0 });
console.log('render', height, this.props.sizes);
setTimeout(() => {
if (height !== this._cache.rowHeight({ index: 0 })) {
console.log('forceUpdate', this._cache.rowHeight({ index: 0 }))
this.forceUpdate()
}
}, 0)
return (
<Grid
ref={ref => this._grid = ref}
deferredMeasurementCache={this._cache}
width={1500}
height={height}
columnCount={5}
columnWidth={this.columnWidth}
rowCount={1}
rowHeight={this._cache.rowHeight}
cellRenderer={this.renderCell}
/>
);
}
所以问题是如何避免forceUpdate?有没有更简洁的方法来使用 react-virtualized 实现具有动态高度的网格标题?
【问题讨论】: