react-simple-infinite-loading

This is an abstraction of howto create an infinite loading list with React and GraphQL.

You can find a demo here.

Step 1: Install it

Install it using the below command:

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

Step 2: Write Code

Here is an example

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>
  )
}

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>
    </>
  )
}

Reference

Read more here.
Find an example here.
Follow code author here.