【发布时间】:2019-01-05 10:04:08
【问题描述】:
我在一个组件中实现了一个反应虚拟化的砌体网格,如下所示:
const MasonrySubmissionRender = (media: InputProps) => {
function cellRenderer({ index, key, parent, style }: MasonryCellProps) {
//const size = (media.submissionsWithSizes && media.submissionsWithSizes.length > index) ? media.submissionsWithSizes[index].size : undefined
//const height = size ? (columnWidth * (size.height / size.width)) : defaultHeight;
function getCard(index: number, extraProps: any) {
var Comp = media.cardElement ? media.cardElement : SubmissionCard
return <Comp submission={media.media[index]} {...media.customCardProps} />
}
return (
<div>
<CellMeasurer
cache={cache}
index={index}
key={key}
parent={parent}>
<div style={style}>
{getCard(index, media.customCardProps)}
</div>
</CellMeasurer>
</div>
);
}
return (
<Masonry
overscanByPixels={1000}
autoHeight={false}
cellCount={media.media.length}
cellMeasurerCache={cache}
cellPositioner={cellPositioner}
cellRenderer={cellRenderer}
style={{ backgroundColor: 'red' }}
height={900}
width={900}
/>
);
};
它呈现一个相当复杂的组件列表,其中包含一系列芯片、css 动画等。
由于这种渲染速度非常慢,即使使用 react-virtualized。
我想实现一个像 imgur.com 这样的系统,其中组件本身不需要立即加载,只显示一个剪影,而我可以让组件准备在后台渲染。
我知道有一种方法可以在滚动时换出组件,但他隐藏了所有组件,包括已经渲染的组件。
【问题讨论】:
标签: reactjs typescript react-virtualized