我正在尝试实现 react-window但我不知道如何传入一个接受它自己属性的组件
如果我有这样的事情
{
items.map(
(item, index) => {
<MyComponent
key={key}
item={item}
/>;
}
);
}
如何制作变量列表?
该示例未显示如何执行此操作
import { VariableSizeList as List } from 'react-window';
// These row heights are arbitrary.
// Yours should be based on the content of the row.
const rowHeights = new Array(1000)
.fill(true)
.map(() => 25 + Math.round(Math.random() * 50));
const getItemSize = index => rowHeights[index];
const Row = ({ index, style }) => (
<div style={style}>Row {index}</div>
);
const Example = () => (
<List
height={150}
itemCount={1000}
itemSize={getItemSize}
width={300}
>
{Row}
</List>
);
最佳答案
这个例子确实令人困惑。 This example展示如何使用 react-window
使用一组数据而不是主页显示的生成示例:
class ComponentThatRendersAListOfItems extends PureComponent {
render() {
// Pass items array to the item renderer component as itemData:
return (
<FixedSizeList
itemData={this.props.itemsArray}
{...otherListProps}
>
{ItemRenderer}
</FixedSizeList>
);
}
}
// The item renderer is declared outside of the list-rendering component.
// So it has no way to directly access the items array.
class ItemRenderer extends PureComponent {
render() {
// Access the items array using the "data" prop:
const item = this.props.data[this.props.index];
return (
<div style={this.props.style}>
{item.name}
</div>
);
}
}
虽然
itemsArray
代码示例中没有提供,表面上你会包含你需要传递给 ItemRenderer
的 Prop 在其中,例如 name
如图所示。这将使您的用法看起来像这样:<ComponentThatRendersAListOfItems
itemsArray={[{ name: "Test 1" }, { name: "Test 2" }]}
/>
关于reactjs - React Window 如何传入组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56244093/