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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-simple-infinite-scroll-patched

v0.0.2

Published

A (patched) brutally simple React infinite scroll component

Downloads

666

Readme

This repo has been forked purely to publish an npm module to solve https://github.com/jaredpalmer/react-simple-infinite-scroll/issues/6


React Simple Infinite Scroll

A brutally simple infinite scroll helper component.

Install

npm install react-simple-infinite-scroll --save

Usage

This component uses a "sentinel" div (with a React ref) that calls getBoundingClientRect() to measure its position and fire off a callback when it becomes visible again. Note: this package eventually becomes somewhat inefficient on very very very large lists because it keeps adding nodes to the DOM. However, this package is extremely valuable for situations when a windowing technique is not possible and when a user is going to realistically scroll a few hundred rows (and not thousands of rows).

Why not use windowing (react-virtualized)?

If you can, you probably should. However, windowing only works when you know the total number of items in the result set ahead of time. This isn't always possible. For example, let's say you have a MongoDB database where you cannot efficiently return the total number of documents in a given query. All your API returns is a cursor (so you can know is if there is another page or not). While this would prevent you from using windowing/react-virtualized, react-simple-infinite-scroll will work just fine.

The Gist

<InfiniteScroll
  throttle={100}
  threshold={300}
  isLoading={this.state.isLoading}
  hasMore={!!this.state.cursor}
  onLoadMore={this.loadMore}
>
  {this.state.myArrayOfItems.map(item => <div>{/* ... */}</div>)}
</InfiniteScroll>
<InfiniteScroll
  throttle={100}
  threshold={300}
  isLoading={this.state.isLoading}
  hasMore={!!this.state.cursor}
  onLoadMore={this.loadMore}
  render={({ sentinel }) => (
    <section>
      {this.state.myArrayOfItems.map(item => <div>{/* ... */}</div>)}
      {sentinel}
    </section>
  )}
/>
// Small react-redux pseudocode
// `storeData` is information extracted from the store
const MyComponent = ({ sentinel, storeData }) => (
  <section>
    {storeData.entities}
    {sentinel}
  </section>
);

const ConnectedComponent = connect(/* ... */)(MyComponent);

<InfiniteScroll
  throttle={100}
  threshold={300}
  isLoading={storeData.isLoading}
  hasMore={storeData.hasMore}
  onLoadMore={() => boundActions.fetchMoreEntities(storeData.cursor)}
  component={ConnectedComponent}
/>

Full Example

import React from 'react'
import { InfiniteScroll } from 'react-simple-infinite-scroll'

export class MyInfiniteScrollExample extends React.Component {
  state = {
    items: [],
    isLoading: true,
    cursor: 0
  }

  componentDidMount() {
    // do some paginated fetch
    this.loadMore()
  }

  loadMore = () => {
    this.setState({ isLoading: true, error: undefined })
    fetch(`https://api.example.com/v1/items?from=${this.state.cursor}`)
      .then(res => res.json())
      .then(
        res => {
          this.setState(state => ({
            items: [...state.items, ...res.items],
            cursor: res.cursor,
            isLoading: false
          }))
        },
        error => {
          this.setState({ isLoading: false, error })
        }
    )
  }

  render() {
    return (
      <div>
        <InfiniteScroll
          throttle={100}
          threshold={300}
          isLoading={this.state.isLoading}
          hasMore={!!this.state.cursor}
          onLoadMore={this.loadMore}
        >
          {this.state.items.length > 0
            ? this.state.items.map(item => (
                <MyListItem key={item.id} title={item.title} />
              ))
            : null}
        </InfiniteScroll>
        {this.state.isLoading && (
          <MyLoadingState />
        )}
      </div>
    )
  }
}

API Reference

Props

hasMore: boolean

Required

Specifies if there are more entities to load.

isLoading: boolean

Required

When true, onLoadMore() will not be executed on scroll.

onLoadMore: () => void

Required

Called when the user has scrolled all the way to the end. This happens when the sentinel has reached the threshold.

threshold?: number

Scroll threshold. Number of pixels before the sentinel reaches the viewport to trigger onLoadMore().

throttle?: number = 64

Defaults to 64. Scroll handler will be executed at most once per the number of milliseconds specified.

Warning: Making this number closer to zero can decrease performance due to a force reflow caused by getBoundingClientRect(), see more properties that can cause this issue in this gist by Paul Irish.

render?: (props: ScrollProps) => React.ReactNode

Callback used for convenient inline rendering and wrapping. Arguments passed Object: { sentinel, children }. Use this if you have a more complex layout where the sentinel needs to be injected.

Warning: The sentinel must be rendered (injected into the DOM) in order for this library to work properly, failing to do so will result in errors and unexpected side effects.

component?: React.ComponentType<ScrollProps>

React component. Similar to the render() prop, this component will receive Object: { sentinel, children } as props. Note that render() prop has precedence over this property, meaning that if both are present, component will not be rendered.

Warning: The sentinel must be rendered (injected into the DOM) in order for this library to work properly, failing to do so will result in errors and unexpected side effects.

Alternatives

Author

Contributors

| jared palmer💻 📖 💡 | pablo garcia💻 📖 💡 | | :---: | :---: |

This project follows the all-contributors specification.