Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-simple-infinite-loading

frinyvonnick362Apache-2.01.1.0

NPM JavaScript Style Guide [![

readme

react-simple-infinite-loading

NPM JavaScript Style Guide gitmoji-changelog

Why?

I wrote an article about creating an infinite loading list with React and GraphQL. Someone pointed out the React implementation of the list was a bit complex. I figure out it was possible to write an abstraction for this particular case. Here it is!

This component aims to stay easy to use. If your use case needs more options I recommend using directly awesome libraries from Brian Vaughn listed in dependencies section.

Demo

You can find a demo here.

Install

npm install --save react-simple-infinite-loading

Usage

import React from 'react'

import InfiniteLoading from 'react-simple-infinite-loading'

function Example({ items, fetchMore, hasMore }) {
  return (
    <div style={{ width: 300, height: 300 }}>
      <InfiniteLoading
        hasMoreItems={hasMore}
        itemHeight={40}
        loadMoreItems={fetchMore}
      >
        {items.map(item => <div key={item}>{item}</div>)}
      </InfiniteLoading>
    </div>
  )
}

Link to example

Dependencies

react-simple-infinite-loading has only three dependencies:

  • react-window is made to display efficiently large lists. It only creates components for the visible elements and reuse nodes.
  • react-window-infinite-loader is a HOC that loads elements just-in-time as user scrolls down the list
  • react-virtualized-auto-sizer helps you displaying your list so it fits the space available in its parent container.

Properties

property name required type description
children yes function The children function should return the jsx for an item of the list. An object is passed as parameter containing item, index, style. You must pass the style to top-level tag of your item's jsx.
items yes array An array of elements. Any type of elements is accepted.
itemHeight yes number The height of an item. All items should have the same height.
itemsCount no number The count of items to be loaded, if known. Prevents the scrollbar from changing its size as the items are being loaded.
hasMoreItems no boolean A boolean that determines if there are still items to load using loadMoreItems function.
loadMoreItems no function A function that will be called each time the list need to load more items.
placeholder no node Any render-able value like strings or React.Nodes to be displayed while children is loading
customScrollbar no boolean A boolean that determines if react-custom-scrollbars is used instead of native one
elementClassName no string A React className prop that will be applied to every child container
ref no ref or function A ref or a callback ref to get component instance so you can call instance's methods (see Methods section)

Methods

scrollTo(scrollOffset: number): void

see FixedSizeList methods section.

scrollToItem(index: number, align: string = "auto"): void

see FixedSizeList methods section.

resetloadMoreItemsCache(): void

Clear previously loaded items from cache.

example

import React from 'react'

import InfiniteLoading from 'react-simple-infinite-loading'

function Example({ items, fetchMore, hasMore }) {
  const ref = React.useRef()
  const scrollToTop = () => {
    if (ref.current) {
      ref.current.scrollTo(0)
    }
  }
  const scrollTo50 = () => {
    if (ref.current) {
      ref.current.scrollToItem(50)
    }
  }
  const resetCache = () => {
    if (ref.current) {
      ref.current.resetloadMoreItemsCache()
    }
  }

  return (
    <>
      <button onClick={scrollToTop}>Scroll to top</button>
      <button onClick={scrollTo50}>Scroll to 50</button>
      <button onClick={resetCache}>Reset cache</button>
      <div style={{ width: 300, height: 300 }}>
        <InfiniteLoading
          hasMoreItems={hasMore}
          itemHeight={40}
          loadMoreItems={fetchMore}
          ref={ref}
        >
          {items.map(item => <div key={item}>{item}</div>)}
        </InfiniteLoading>
      </div>
    </>
  )
}

License

Apache-2.0 © frinyvonnick

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Yvonnick FRIN

📖

Augusto

📖

Henrique Martins

📖

Yoann Prot

📖

Jimmy Kasprzak

💻

Kerem Hallaç

💻

Tim

📖 💻

Giancarlo França

💻

Giles

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

changelog

Changelog

1.1.0 (2020-09-18)

Added

  • ✨ Add an elementClassName prop (#43) [e37e615]

Changed

  • 📌 Refresh lockfiles [53f4d08]
  • 💄 Improve example style [49e6a71]

Fixed

1.0.0 (2020-01-13)

Changed

  • 📌 Move to yarn and generate lock file [1df7044]
  • ♻️ Update example to use an api [a115fda]

Fixed

  • 🐛 Avoid character duplication in example [2ab85ec]

1.0.0-alpha.8 (2020-01-06)

Added

  • ✨ Expose internal methods using ref (#38) [50fb563] (by Yvonnick FRIN)
  • ✨ Add the possibility to customize the scrollbar (#32) [41c4616] (by Yvonnick FRIN)

Changed

  • 🚨 Remove React warning about forwardRef (#33) [3b83ea9] (by Giles)

1.0.0-alpha.7 (2019-10-25)

Added

  • ✨ Add an itemsCount prop to InfiniteLoader (#28) [57efe2a]

Removed

  • 🔥 Remove allcontributors badge [f64328d]

Miscellaneous

  • 📝 Add gitmoji-changelog badge [d90c3a5]
  • 👥 Add gVirtu as a contributor (#30) [6cf2229]

1.0.0-alpha.6 (2019-10-18)

Added

Miscellaneous

  • 📝 Improve example functionality and ui (#25) [4913a39]
  • 📝 Improve example documentation (#23) [18218a6]
  • 📝 Add demo link to readme (#27) [cde6a4b]
  • 👥 Update trybick contributions type (#22) [d4f3bd6]
  • 👥 Add trybick as a contributor (#21) [1b744a8]
  • 📝 Fix import statement in usage section (#19) [c391d9b]
  • 📝 Change contributions type in contributors section [94b8561]
  • 👥 Add keremh as a contributor (#16) [e33ec7d]
  • 👥 Add Orodan as a contributor (#15) [08c1c5c]

1.0.0-alpha.5 (2019-10-10)

Added

  • ✨ Add the possibility to pass down ref to react-window-infinite-loader (#9) [3795271]

Changed

  • ♻️ Replace children function by an array (#7) [22357c5]

Miscellaneous

  • 📦 Define dependencies as external in rollup config (#12) [5d4a10b]
  • 📝 Add contributors section in README.md #10 (#11) [2446fa2]

1.0.0-alpha.4 (2019-09-18)

Changed

  • ♻️ Remove the need to pass style to children function (#3) [dd2b48c]

Miscellaneous

  • 📝 Create code of conduct [1c469e7]
  • 📄 Set license content [4b6620e]

1.0.0-alpha.3 (2019-08-26)

Added

  • 🎉 Add a first implementation [7c52811]

Changed

Removed

  • 🔥 Remove unused dependencies [364a689]

Miscellaneous

  • 📝 Improve example and fix typo [8a53ca5]
  • 📝 Simplify example and fix some typos (#1) [b827dac]
  • 📝 Improve documentation [2963242]
  • Initial commit [9bbf6c4]