Javascript react虚拟化网格从服务中检索图像,但直到滚动操作后才呈现
我正在构建一个组件,用于显示从react虚拟化网格中的服务器检索到的数据。除了一个问题外,一切正常:当组件第一次渲染时,检索到的图像不会显示。但是,一旦发生滚动操作,它们将变为可用 我认为这与我的承诺的工作方式有关,但我似乎找不到解决办法 图像数据已成功检索,状态已成功更新,但仍不显示 我的理解是,当网格中的任何单元格使用“rowRenderer”回调进行渲染时,“whenSectionRendered”回调应触发,从而导致以下顺序:Javascript react虚拟化网格从服务中检索图像,但直到滚动操作后才呈现,javascript,reactjs,asynchronous,react-virtualized,Javascript,Reactjs,Asynchronous,React Virtualized,我正在构建一个组件,用于显示从react虚拟化网格中的服务器检索到的数据。除了一个问题外,一切正常:当组件第一次渲染时,检索到的图像不会显示。但是,一旦发生滚动操作,它们将变为可用 我认为这与我的承诺的工作方式有关,但我似乎找不到解决办法 图像数据已成功检索,状态已成功更新,但仍不显示 我的理解是,当网格中的任何单元格使用“rowRenderer”回调进行渲染时,“whenSectionRendered”回调应触发,从而导致以下顺序: 调用“LoadNewImagesBetweenIndexes
class HistoryDisplay扩展了PureComponent{
建造师(道具){
超级(道具);
此.state={
宽度:1000,
以前的IDXStart:-1,
上一个IDXStop:-1,
spotHistoryData:“”
}
this.cellRenderer=this.cellRenderer.bind(this);
this.whenSectionRendered=this.whenSectionRendered.bind(this);
this.loadHistImage=this.loadHistImage.bind(this);
this.loadHistory=this.loadHistory.bind(this);
this.removeHistImages=this.removeHistImages.bind(this);
}
componentDidMount(){
此参数为.resetRowWidthIfRequired();
this.loadHistory(this.props.spotId);
}
componentDidUpdate(prevProps){
此.resetPrevidXIFrequeired(prevProps);
如果(prevProps.spotId!==this.props.spotId){
this.loadHistory(this.props.spotId);
}
}
...
异步loadHistImage(imageId){
返回imageService.retrieveImage(imageId)
。然后(数据=>{
const newHistData=this.state.spotHistoryData.map(x=>{
if(x.id\u image\u decision===imageId){
x、 图像_决策=数据;
}
返回x;
});
此.updatesPostistoryData(newHistData);
});
}
更新历史数据(newHistoryData){
这是我的国家({
spotHistoryData:newHistoryData
});
}
whenSectionRendered({rowOverscanStartIndex,rowOverscanStopIndex}){
const{previousIdxStart,previousIdxStop}=this.state;
此文件在索引之间加载新图像(
this.state.spotHistoryData,
rowOverscanStartIndex,
行扫描指数);
此.ClearImages在指示器之间不可见(
rowOverscanStartIndex,
行数指数,
以前的idxstart,
以前的idxstop);
}
在索引之间加载新图像(数组、startIdx、stopIdx){
对于(让rowIndex=startIdx;rowIndex
class HistoryDisplay extends PureComponent{
constructor(props) {
super(props);
this.state = {
width: 1000,
previousIdxStart: -1,
previousIdxStop: -1,
spotHistoryData: ''
}
this.cellRenderer = this.cellRenderer.bind(this);
this.whenSectionRendered = this.whenSectionRendered.bind(this);
this.loadHistImage = this.loadHistImage.bind(this);
this.loadHistory = this.loadHistory.bind(this);
this.removeHistImages = this.removeHistImages.bind(this);
}
componentDidMount() {
this.resetRowWidthIfRequired();
this.loadHistory(this.props.spotId);
}
componentDidUpdate(prevProps) {
this.resetPrevIdxIfRequired(prevProps);
if(prevProps.spotId !== this.props.spotId){
this.loadHistory(this.props.spotId);
}
}
...
async loadHistImage(imageId) {
return imageService.retrieveImage(imageId)
.then(data => {
const newHistData = this.state.spotHistoryData.map(x => {
if (x.id_image_decision === imageId){
x.image_decision = data;
}
return x;
});
this.updateSpotHistoryData(newHistData);
});
}
updateSpotHistoryData(newHistoryData) {
this.setState({
spotHistoryData: newHistoryData
});
}
whenSectionRendered({rowOverscanStartIndex, rowOverscanStopIndex}) {
const { previousIdxStart, previousIdxStop } = this.state;
this.loadNewImagesBetweenIndices(
this.state.spotHistoryData,
rowOverscanStartIndex,
rowOverscanStopIndex);
this.clearImagesBetweenIndicesNotInView(
rowOverscanStartIndex,
rowOverscanStopIndex,
previousIdxStart,
previousIdxStop);
}
loadNewImagesBetweenIndices(array, startIdx, stopIdx) {
for(let rowIndex=startIdx; rowIndex<stopIdx; rowIndex++) {
this.loadHistImage(array[rowIndex].id_image_decision)
}
this.setState({
previousIdxStart: startIdx,
previousIdxStop: stopIdx
});
}
cellRenderer({columnIndex, key, rowIndex, style}) {
const { spotHistoryData } = this.state;
return(
<div key={key} >
{this.createCell(spotHistoryData, columnIndex, rowIndex, style)}
</div>
);
}
createCell(items, columnIndex, rowIndex, style){
const formattedTimestamp = (new Date(items[rowIndex].time_stamp_decision)).toLocaleString();
const btnColor = 'white';
switch(columnIndex){
case 0:
return this.renderHistoryImageButton(
this.createCellId(columnIndex, rowIndex),
btnColor,
style,
formattedTimestamp,
items[rowIndex].image_decision
);
case 1:
return this.renderHistoryDataButton(
this.createCellId(columnIndex, rowIndex),
btnColor,
style,
'Timestamp',
formattedTimestamp
);
...
}
}
render() {
const { height, rowHeight } = this.props;
const { width, spotHistoryData } = this.state;
return(
<Col>
<Grid
width={width}
height={height}
rowHeight={rowHeight}
cellRenderer={this.cellRenderer}
rowCount={spotHistoryData.length}
columnCount={7}
columnWidth={this.getCellWidth}
estimatedColumnSize={100}
overscanRowCount={3}
overscanColumnCount={3}
onSectionRendered={this.whenSectionRendered}
noContentRenderer={this.renderNoContent}
/>
</Col>
);
}
}