从我对文档的阅读来看,React Virtualized 库目前似乎不支持这一点
我很想知道文档的哪一部分给了你这种印象。您的用例听起来像是一个反应虚拟化的设备可以处理。:)
该Collection
组件似乎可能很接近
Collection
用于其他用途。也许这些来自我最近一次会议演讲的幻灯片可以澄清一点。基本上,Collection
用于非线性数据(例如甘特图、Pinterest 布局等)。它更灵活,但这是以性能为代价的。您的用例听起来非常适合List
. :)
更新的答案
您可以使用List
andAutoSizer
来完成此操作。您只需要使用可用宽度和项目高度来计算行数。不太复杂。:)
这是一个 Plunker 示例,源代码如下:
const { AutoSizer, List } = ReactVirtualized
const ITEMS_COUNT = 100
const ITEM_SIZE = 100
// Render your list
ReactDOM.render(
<AutoSizer>
{({ height, width }) => {
const itemsPerRow = Math.floor(width / ITEM_SIZE);
const rowCount = Math.ceil(ITEMS_COUNT / itemsPerRow);
return (
<List
className='List'
width={width}
height={height}
rowCount={rowCount}
rowHeight={ITEM_SIZE}
rowRenderer={
({ index, key, style }) => {
const items = [];
const convertedIndex = index * itemsPerRow;
for (let i = convertedIndex; i < convertedIndex + itemsPerRow; i++) {
items.push(
<div
className='Item'
key={i}
>
Item {i}
</div>
)
}
return (
<div
className='Row'
key={key}
style={style}
>
{items}
</div>
)
}
}
/>
)
}}
</AutoSizer>,
document.getElementById('example')
)
初步答案
这是我会做的,或多或少:
export default class Example extends Component {
static propTypes = {
list: PropTypes.instanceOf(Immutable.List).isRequired
}
constructor (props, context) {
super(props, context)
this._rowRenderer = this._rowRenderer.bind(this)
this._rowRendererAdapter = this._rowRendererAdapter.bind(this)
}
shouldComponentUpdate (nextProps, nextState) {
return shallowCompare(this, nextProps, nextState)
}
render () {
const { list } = this.props
return (
<AutoSizer>
{({ height, width }) => (
<CellMeasurer
cellRenderer={this._rowRendererAdapter}
columnCount={1}
rowCount={list.size}
width={width}
>
{({ getRowHeight }) => (
<List
height={height}
rowCount={list.size}
rowHeight={getRowHeight}
rowRenderer={this._rowRenderer}
width={width}
/>
)}
</CellMeasurer>
)}
</AutoSizer>
)
}
_getDatum (index) {
const { list } = this.props
return list.get(index % list.size)
}
_rowRenderer ({ index, key, style }) {
const datum = this._getDatum(index)
return (
<div
key={key}
style={style}
>
{datum.name /* Or whatever */}
</div>
)
}
_rowRendererAdapter ({ rowIndex, ...rest }) {
return this._rowRenderer({
index: rowIndex,
...rest
})
}
}