【发布时间】:2022-01-22 21:23:46
【问题描述】:
我正在尝试渲染响应式图像网格,它可能包含不同大小的图像。听起来 Masonry 组件非常适合这种情况,但不确定我能否在我的应用程序中使用这个 example?它看起来与它所居住的地方完全耦合,我尝试获取相关部分,但我无法开始工作。
另外,我尝试使用wizard 生成相关代码,并得到了这个示例:
<AutoSizer>
{({ height, width }) => (
<CellMeasurer
cellRenderer={yourCellRenderer}
columnCount={numColumns}
rowCount={numRows}
width={width}
>
{({ getRowHeight }) => (
<Grid
cellRenderer={({ columnIndex, key, rowIndex, style }) => <div key={key} style={style}>...</div>}
columnCount={numColumns}
columnWidth={({ index }) => 100}
height={height}
rowCount={numRows}
rowHeight={getRowHeight}
width={width}
/>
)}
</CellMeasurer>
)}
</AutoSizer>
但是我应该用什么来代替yourCellRenderer、getRowHeight?
基于互联网上的一些示例,我构建了以下代码:
<div className="media-viewer" style={{height: "100vh"}}>
<AutoSizer>
{({height, width}) => (
<Grid
cellRenderer={({columnIndex, key, rowIndex, style}) => {
if (!result[rowIndex][columnIndex]) return <div key={key} style={style}></div>;
return (
<div key={key} style={style}>
<MediaItem key={result[rowIndex][columnIndex].id} app={app}
item={result[rowIndex][columnIndex]}/>
</div>
);
}}
columnCount={5}
columnWidth={250}
height={height}
rowCount={result.length}
rowHeight={250}
width={width}
/>
)}
</AutoSizer>
</div>
如果有人能够为我提供基于 react-virtualize 的响应式网格的可靠示例,或者我可以改进当前代码的地方,我将不胜感激。
最好的问候。
【问题讨论】:
标签: reactjs react-virtualized react-masonry