1.2.6 • Published 2 years ago

react-infinite-scroller v1.2.6

Weekly downloads
284,708
License
MIT
Repository
github
Last release
2 years ago

React Infinite Scroller

npm npm npm

Infinitely load a grid or list of items in React. This component allows you to create a simple, lightweight infinite scrolling page or element by supporting both window and scrollable elements.

  • ⏬ Ability to use window or a scrollable element
  • 📏 No need to specify item heights
  • 💬 Support for "chat history" (reverse) mode
  • ✅ Fully unit tested and used in hundreds of production sites around the world!
  • 📦 Lightweight alternative to other available React scroll libs ~ 2.2KB minified & gzipped

Installation

npm install react-infinite-scroller --save
yarn add react-infinite-scroller

How to use

import InfiniteScroll from 'react-infinite-scroller';

Window scroll events

<InfiniteScroll
    pageStart={0}
    loadMore={loadFunc}
    hasMore={true || false}
    loader={<div className="loader" key={0}>Loading ...</div>}
>
    {items} // <-- This is the content you want to load
</InfiniteScroll>

DOM scroll events

<div style="height:700px;overflow:auto;">
    <InfiniteScroll
        pageStart={0}
        loadMore={loadFunc}
        hasMore={true || false}
        loader={<div className="loader" key={0}>Loading ...</div>}
        useWindow={false}
    >
        {items}
    </InfiniteScroll>
</div>

Custom parent element

You can define a custom parentNode element to base the scroll calulations on.

<div style="height:700px;overflow:auto;" ref={(ref) => this.scrollParentRef = ref}>
    <div>
        <InfiniteScroll
            pageStart={0}
            loadMore={loadFunc}
            hasMore={true || false}
            loader={<div className="loader" key={0}>Loading ...</div>}
            useWindow={false}
            getScrollParent={() => this.scrollParentRef}
        >
            {items}
        </InfiniteScroll>
    </div>
</div>

Props

NameRequiredTypeDefaultDescription
children      YesNode           Anything that can be rendered (same as PropType's Node)
loadMore      YesFunction           A callback when more items are requested by the user. Receives a single parameter specifying the page to load e.g. function handleLoadMore(page) { /* load more items here */ } }
elementComponent'div'Name of the element that the component should render as.
hasMoreBooleanfalseWhether there are more items to be loaded. Event listeners are removed if false.
initialLoadBooleantrueWhether the component should load the first set of items.
isReverseBooleanfalseWhether new items should be loaded when user scrolls to the top of the scrollable area.
loaderComponentA React component to render while more items are loading. The parent component must have a unique key prop.
pageStartNumber0The number of the first page to load, With the default of 0, the first page is 1.
getScrollParentFunctionOverride method to return a different scroll listener if it's not the immediate parent of InfiniteScroll.
thresholdNumber250The distance in pixels before the end of the items that will trigger a call to loadMore.
useCaptureBooleanfalseProxy to the useCapture option of the added event listeners.
useWindowBooleantrueAdd scroll listeners to the window, or else, the component's parentNode.

Troubleshooting

Double or non-stop calls to loadMore

If you experience double or non-stop calls to your loadMore callback, make sure you have your CSS layout working properly before adding this component in. Calculations are made based on the height of the container (the element the component creates to wrap the items), so the height of the container must equal the entire height of the items.

.my-container {
  overflow: auto;
}

Some people have found success using react-infinite-scroll-component.

But you should just add an isLoading prop!

This component doesn't make any assumptions about what you do in terms of API calls. It's up to you to store whether you are currently loading items from an API in your state/reducers so that you don't make overlapping API calls.

loadMore() {
  if(!this.state.isLoading) {
    this.props.fetchItems();
  }
}
@mindbox/frontend@contiago/toolbar@borderfreefinancial/revo-consumer@borderfreefinancial/revo-core@borderfreefinancial/revo-core-api@borderfreefinancial/revo-web-video-test@corva/uijoyjet-space-chatreactjs-99unowebapp-userdafunextension-chromeflowbuilder-testmegvii-qdcloud-component@follett/common-uiexpa-live-chat-plugincrm-appminsilo-react-clientcopadotoca-chat99uno-react-js299uno-reactjsvib-librarygatsby-theme-factlypcs-notif-and-msgcl2-frontxr3-spokexrchat-spokerayplus_fus_frontyw-goodscaptain-dashboardswapl-workssimple-antd-management-fast-frameworkglobal-navigationsid-npm-test3sid-npmtest-1sid-npmtest-2sid-test-create-npmopenvsx-webui-stagingonecam-sharedanm.pa.search.widgetreact-single-bmsscrm-admin@infinitebrahmanuniverse/nolb-react-insupport-worksuiteluck-design-test@abtasty/pulsar-common-ui@everything-registry/sub-chunk-2561buzzmsg-test@decenter/melon-mailfccs-sdkfhxumifinone-web-rcfinone-web-testfl-business-root-managerfilum-event-queryfrontend-challengeenderv-tsml-uiepub-listevmagic-boxexample-package-02imsdk-bbchatimsdkwebimsdkwebtestimsk-bbchatilluminsightilluminsight-webisigningsdkistox-sharedinfinidesk-web-widgetjctest1718192021jdcloudmuijdclouduijam-cmsduodashi-webelectraeo@evkj/magic-box@essex/hierarchy-browser@essex-js-toolkit/hierarchy-browsergsp-reactgatsby-theme-octahedroidgc-common-ui@edgelogistics/edge-uihealthshare_admin@firesoon/bc-settle-mrhp-components@firesoon/ant-ui@firesoon/bc-bl-drawer-diphhz-commongac-pro-spegennlife-douigeneric-packagesgeru-editorght-react-filter-searchght-react-searchght-reactght-react-filterght-react-filter-demoghost-design-system@dzc/scroll-loading@filum-ai/platform-apps-ui
1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.2.11

8 years ago

0.2.10

8 years ago

0.2.9

8 years ago

0.2.8

8 years ago

0.2.7

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago