简体   繁体   English

如何逐步渲染反应组件?

[英]How to progressively render react components?

I'm working with lists that will likely be in the range of 500 up to maybe 5000 items. 我正在处理的列表可能在500到5000个项目之间。 Each item in the list will show as a component, like so: 列表中的每个项目都将显示为一个组件,如下所示:

List 名单

render() {
  return (
    <div className="ItemList">
      <Info items={this.props.items} />
      <ul>
        {this.props.items.map( item =>
          <Item item={item} key={item._id} refresh={this.props.refresh} />
        )}
      </ul>
    </div>
  );
}

Item 项目

render() {
  var item = this.props.item;

  return (
    <li onClick={() => this.setState({showInfo: !this.state.showInfo})} 
                 className="Item">
      <h3 className={this.state.showInfo ? "item-title-bar active" : "item-title-bar"}>
        <div>
          <div className="item-category">
            {item.category} 
          </div>
          <div className="item-name">
            {item.name}
          </div>
        </div>
      </h3>

      {this.state.showInfo &&
        <ItemInfo item={this.props.item} refresh={this.props.refresh} />
      }
    </li>
  );
}

Once one of these lists gets up to around 1000 items, it's noticeably slow when I click to show a different list. 一旦这些列表中的一个列表最多容纳1000个项目,当我单击以显示其他列表时,它的速度就会明显变慢。 Perf tools are showing me 90-150 ms for displaying this list at 1000 or 2000 items. Perf工具向我显示90-150毫秒,以1000或2000个项目显示此列表。 Not sure I can get around that as long as I'm rendering them. 不确定只要渲染它们就可以解决。

So, what I'm trying to do: 所以,我想做的是:

  1. Can I let the initial items update, then render others in the background, while the app remains responsive? 我可以让初始项目更新,然后在应用程序保持响应的同时在后台渲染其他项目吗?

  2. How can I show initial items, then load more as the user scrolls down the page? 如何显示初始项目,然后在用户向下滚动页面时加载更多项目?

If neither option works, I'll probably try to load a few, then add a show more or show all button at the bottom of the list. 如果这两个选项都不起作用,我可能会尝试加载一些,然后在列表底部添加显示更多或全部显示按钮。 Want to make this as seamless as possible though, open to other suggestions as well. 想要尽可能地做到无缝,也欢迎其他建议。

react-virtualized would be my first choice when dealing with a virtual list. 当处理虚拟列表时, react-virtualized将是我的首选。 Lot of examples here: https://bvaughn.github.io/react-virtualized/#/components/List 这里有很多例子: https : //bvaughn.github.io/react-virtualized/#/components/List

Pretty simple if you know the heights of the items ahead of time, but can use the CellMeasurer component if you don't. 如果您提前知道项目的高度,则非常简单,但如果不知道,可以使用CellMeasurer组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM