reactjs - React Window 如何传入组件?

标签 reactjs react-window

我正在尝试实现 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/

相关文章:

reactjs - 将Draft JS实现到Next JS中,文本编辑器不会显示

javascript - react-router-dom 中的链接删除 css

javascript - React 组件构造函数何时运行与生命周期相关?

node.js - ReactJS + Axios + NodeJS - _id : Cannot read property 'ownerDocument' of null

reactjs - 如何设置material-ui持卡人头像?

javascript - react 窗口滚动到底部

reactjs - React-window-infinite-loader Material -ui 自动完成

reactjs - 使用 Autosizer react 窗口