0
react-virtualized的InfiniteLoader
需要種子數據才能正常工作嗎?以下是我的組件:使用react-virtualized InfiniteLoader
class Bookmarks extends PureComponent {
constructor(props) {
super(props);
this.loaded = {
cursor: null,
data: []
};
this._isRowLoaded = this._isRowLoaded.bind(this);
this._loadMoreRows = this._loadMoreRows.bind(this);
this._rowRenderer = this._rowRenderer.bind(this);
}
render() {
const size = this.loaded.data.length;
return (
<InfiniteLoader
isRowLoaded={this._isRowLoaded}
loadMoreRows={this._loadMoreRows}
rowCount={size}>
{({onRowsRendered, registerChild}) =>
<AutoSizer>
{({width, height}) =>
<List
ref={registerChild}
height={height}
onRowsRendered={onRowsRendered}
rowCount={size}
rowHeight={30}
rowRenderer={this._rowRenderer}
width={width}
/>}
</AutoSizer>}
</InfiniteLoader>
);
}
_isRowLoaded({index}) {
return !!this.loaded.data[index];
}
_loadMoreRows({startIndex, stopIndex}) {
fetch('/api/bookmarks').then((response) => {
return response.json();
}).then((json) => {
this.loaded = {
cursor: json.cursor,
data: this.loaded.data.push(...json.data),
};
});
}
_rowRenderer({index, key, style}) {
return (
<div key={key} style={style}>{this.loaded.data[index]}</div>
);
}
}
在render()
,size
最初是零,因爲沒有數據着呢,我假設組件將調用_loadMoreRows
-apparently沒有。
這是我認爲是邏輯的流程(這是不正確的):創建
- 組件
_loadMoreRows
被稱爲(第一時間)- 當
_loadMoreRows
返回的承諾是解決了,檢查每個裝載的行與_isRowLoaded
- 渲染每一行