1.0.9 • Published 1 year ago

react-window-infinite-loader v1.0.9

Weekly downloads
87,990
License
MIT
Repository
github
Last release
1 year ago

react-window-infinite-loader

InfiniteLoader component inspired by react-virtualized but for use with react-window

If you like this project, 🎉 become a sponsor or ☕ buy me a coffee

Install

# Yarn
yarn add react-window-infinite-loader

# NPM
npm install --save react-window-infinite-loader

Documentation

NameTypeDescription
children({ onItemsRendered: Function, ref: React$Ref }) => React$NodeRender prop. See below for example usage.
isItemLoaded(index: number) => booleanFunction responsible for tracking the loaded state of each item.
itemCountnumberNumber of rows in list; can be arbitrary high number if actual number is unknown.
loadMoreItems(startIndex: number, stopIndex: number) => Promise<void>Callback to be invoked when more rows must be loaded. It should return a Promise that is resolved once all data has finished loading.
minimumBatchSize?numberMinimum number of rows to be loaded at a time; defaults to 10. This property can be used to batch requests to reduce HTTP requests.
threshold?numberThreshold at which to pre-fetch data; defaults to 15. A threshold of 15 means that data will start loading when a user scrolls within 15 rows.

Example usage

The snippet below shows a basic example of how the InfiniteLoader can be used to wrap either a FixedSizeList or VariableSizeList from react-window.

// This value is arbitrary.
// If you know the size of your remote data, you can provide a real value.
// You can also increase this value gradually (as shown in the example below).
const itemCount = 1000;

<InfiniteLoader
  isItemLoaded={isItemLoaded}
  itemCount={itemCount}
  loadMoreItems={loadMoreItems}
>
  {({ onItemsRendered, ref }) => (
    <FixedSizeList
      itemCount={itemCount}
      onItemsRendered={onItemsRendered}
      ref={ref}
      {...otherListProps}
    />
  )}
</InfiniteLoader>

Try it on Code Sandbox

Creating an infinite loading list

The InfiniteLoader component was created to help break large data sets down into chunks that could be just-in-time loaded as they were scrolled into view. It can also be used to create infinite loading lists (e.g. Facebook or Twitter). Here's a basic example of how you might implement that:

function ExampleWrapper({
  // Are there more items to load?
  // (This information comes from the most recent API request.)
  hasNextPage,

  // Are we currently loading a page of items?
  // (This may be an in-flight flag in your Redux store for example.)
  isNextPageLoading,

  // Array of items loaded so far.
  items,

  // Callback function responsible for loading the next page of items.
  loadNextPage
}) {
  // If there are more items to be loaded then add an extra row to hold a loading indicator.
  const itemCount = hasNextPage ? items.length + 1 : items.length;

  // Only load 1 page of items at a time.
  // Pass an empty callback to InfiniteLoader in case it asks us to load more than once.
  const loadMoreItems = isNextPageLoading ? () => {} : loadNextPage;

  // Every row is loaded except for our loading indicator row.
  const isItemLoaded = index => !hasNextPage || index < items.length;

  // Render an item or a loading indicator.
  const Item = ({ index, style }) => {
    let content;
    if (!isItemLoaded(index)) {
      content = "Loading...";
    } else {
      content = items[index].name;
    }

    return <div style={style}>{content}</div>;
  };

  return (
    <InfiniteLoader
      isItemLoaded={isItemLoaded}
      itemCount={itemCount}
      loadMoreItems={loadMoreItems}
    >
      {({ onItemsRendered, ref }) => (
        <FixedSizeList
          itemCount={itemCount}
          onItemsRendered={onItemsRendered}
          ref={ref}
          {...props}
        >
          {Item}
        </FixedSizeList>
      )}
    </InfiniteLoader>
  );
}

Try it on Code Sandbox

Advanced usage

Some use cases require cached items to be reset. For example, after a list has been sorted, previously cached items may be invalid. You can let InfiniteLoader know that it needs to reload cached items by calling the resetloadMoreItemsCache method.

function ExampleWrapper({
  // ...
  sortOrder,
}) {
  // We create a reference for the InfiniteLoader
  const infiniteLoaderRef = useRef(null);
  const hasMountedRef = useRef(false);

  // Each time the sort prop changed we called the method resetloadMoreItemsCache to clear the cache
  useEffect(() => {
    // We only need to reset cached items when "sortOrder" changes.
    // This effect will run on mount too; there's no need to reset in that case.
    if (hasMountedRef.current) {
      if (infiniteLoaderRef.current) {
        infiniteLoaderRef.current.resetloadMoreItemsCache();
      }
    }
    hasMountedRef.current = true;
  }, [sortOrder]);
  
  // ...

  // We passed down the ref to the InfiniteLoader component
  return (
    <InfiniteLoader
      ref={infiniteLoaderRef}
      isItemLoaded={isItemLoaded}
      itemCount={itemCount}
      loadMoreItems={loadMoreItems}
    >
      {({ onItemsRendered, ref }) => (
        // ...
      )}
    </InfiniteLoader>
  );
}

Try it on Code Sandbox

License

MIT © bvaughn

@procore/core-react@invisionag/components@elliemae/ds-mobile@elliemae/ds-shuttle@mateuszmigas/dropdown@open-raven/react-styleguidekhodroinja-ui-coremoneko.consolenotes-fwreact-highlightable-datasheetmdlinx-smartestdoc@infinitebrahmanuniverse/nolb-react-wi@oerlikon/ui@compassdigital/compassdigital.components.distilr@everything-registry/sub-chunk-2624react-publishable-component-sunilprox-uisg-mobilesleek-ui@paprika/data-table@rafty/ui@rangodev/ui@rango-test/uisimbrary@simspace/monorailreact-native-ridge-list@nexray/appreact-infinite-virtual-treereact-components-bundlereact-formbuilder-v2redux-connected-componentsreact-window-pull-refreshreact-simple-infinite-loading@nexusui/componentsreact-unifiedreact-table-flash-uiselect-mui@thinkincoin/swap@tractorzoom/components@xtreamsrl/react-ui-kit@xtreamsrl/react-form-input-kit@zagrajmy/appsdk-chat-widgetsdk-chat-widget-test@tarojsx/librarysushiwebetsoo-reactgd-test-chainlit-uilatitude-flexportmui-data-datagrid@economic/taco@economic/gaudimycel-widgetlth-toolshx-react-componentsinforiver-enterpriseinforiver-standardinfinite-nftmanktestmetadata-react@elliemae/ds-legacy-shuttle@elliemae/ds-legacy-mobilehacked-react-twemoji-pickerimuikodo-art-tablegm_static_x_mesish-ui@flowfact/ui-componentsonekijs-uiunbxd-react-components@aics/fms-file-explorer-core@ainias42/react-bootstrap-mobile@anyvision/anv-ui-componentsbeatchain-artists-reactuhnoxi-music-webuhnoxi-music-web-build@adviseinc-motor-js/componentstnpulse-react-twemoji-picker@uitoolkit/ui-components@unbxd-ui/unbxd-react-components@zuludmmswap/interface@ukpc-lib/react@virtualized-table/table@habx/ui-table@goto-company/apsara@darren-z-m-lin/trojan-web-manager-client@gaut/ui@frontapp/ui-kitwhat-a-dragblondie-platform-webderiv-p2pdigital-motors-web-client@caisy/league@carlospence/react-fluid-table@itwin/changed-elements-react@jayjonesdev/react-material-ui-library@krowdy/kds-views@krowdy-ui/views@dalmo/sanity-plugin-media@deriv/p2p
1.0.9

1 year ago

1.0.8

2 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago