npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-infinite-auto-scroller

v0.1.8

Published

react长列表动态优化组件

Downloads

12

Readme

react-infinite-auto-scroller

react长列表自动优化组件

👉 demo

👉 react-infinite-auto-scroller

参数

| 名称 | 类型 | 必填 | 备注 | | -- | -- | -- | -- | | id | string | 是 | 作为列表项的key,必填且保证唯一,否则无法渲染虚拟列表 | | renderItem | (item: any, index: number) => Element | 是 | 渲染每个列表项的函数 | | list | any[] | 是 | 列表数据 | | root | 类名或id | 否 | 将scroll事件监听绑定到指定节点,默认为window |

api

emitReportHeight(height: number, loaded: boolean) => void

该方法将用props传入列表项组件内,height表示列表项的高度,loaded表示是否加载完毕。

加载完毕后需要主动调用该方法确定高度不再变化后才会被合并为空节点。

注意当异步资源加载失败,也需要主动上报高度 否则不会被合并为空节点,如果存在大量无法合并的节点将会导致列表变卡。

用法:

npm install react-infinite-auto-scroller

import InfiniteScrollItem, { clearHeightCache } from './scrollItem';
// ...
  componentWillUnmount() {
    clearHeightCache(); // 当列表卸载时,主动清除列表高度缓存。如果没有清除,下次加载此列表时可以直接使用该高度缓存,跳过获取高度逻辑。
  }

  render() {
    return (
      <InfiniteScroll
        id={'id'}
        list={this.state.list}
        renderItem={(item, index) => { // 注意:item与list中的项不是同一个值,而是通过浅拷贝之后的列表项
          // 注意item中添加了emitReportHeight事件,如需要主动改变父节点高度,请在改变的组件内手动调用
          return (
            <Item {...item} />
          );
        }}/>
    );
  }
// ...

注意此处list中的列表项必须包含属性名为id的列表唯一值,不建议使用索引,因为当从头部插入或者删除中间某一项时会导致节点高度匹配数据错误。

列表项需要主动上报并确认组件高度:

export default class Item extends Component {
  ref = React.createRef();

  componentDidMount() {
    // 注意如果高度是由异步资源决定的,那应该在异步资源加载完成之后再上报高度,因为高度一旦上报,如果此时元素未在可视区域,该元素就会被合并为一个空节点
    if (!this.props.img) {
      this.reportHeight(true); // 注意即使这个组件是同步加载的也需要上报loaded为true才会合并进空节点
    }
  }

  render() {
    const { text, img } = this.props;
    return (
      <div className="item" ref={this.ref}>
        <p className="text">{text}</p>
        {img ? <img className="img" src={img} alt="" onLoad={this.imgLoad}/> : null}
      </div>
    );
  }

  private imgLoad = (e) => {
    this.reportHeight(true); // 这里高度变化后主动上报高度。
  };

  private reportHeight(loaded) {
    const height = this.ref.current?.getBoundingClientRect().height;
    this.props.emitReportHeight && this.props.emitReportHeight(height, loaded); // 这里高度变化后主动上报高度。
  }
}

注意事项

  1. 请不要在组件挂载&卸载时处理特殊逻辑,比如打点等,因为优化列表后会反复移除插入组件。

  2. 关注依赖异步元素决定高度的列表项,当加载失败时选择重试或者上报确认高度不再变化。