• Stars
    star
    2,822
  • Rank 16,132 (Top 0.4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An awesome Infinite Scroll component in react.

react-infinite-scroll-component npm npm

All Contributors

A component to make all your infinite scrolling woes go away with just 4.15 kB! Pull Down to Refresh feature added. An infinite-scroll that actually works and super-simple to integrate!

Install

  npm install --save react-infinite-scroll-component

  or

  yarn add react-infinite-scroll-component

  // in code ES6
  import InfiniteScroll from 'react-infinite-scroll-component';
  // or commonjs
  var InfiniteScroll = require('react-infinite-scroll-component');

Using

<InfiniteScroll
  dataLength={items.length} //This is important field to render the next data
  next={fetchData}
  hasMore={true}
  loader={<h4>Loading...</h4>}
  endMessage={
    <p style={{ textAlign: 'center' }}>
      <b>Yay! You have seen it all</b>
    </p>
  }
  // below props only if you need pull down functionality
  refreshFunction={this.refresh}
  pullDownToRefresh
  pullDownToRefreshThreshold={50}
  pullDownToRefreshContent={
    <h3 style={{ textAlign: 'center' }}>&#8595; Pull down to refresh</h3>
  }
  releaseToRefreshContent={
    <h3 style={{ textAlign: 'center' }}>&#8593; Release to refresh</h3>
  }
>
  {items}
</InfiniteScroll>

Using scroll on top

<div
  id="scrollableDiv"
  style={{
    height: 300,
    overflow: 'auto',
    display: 'flex',
    flexDirection: 'column-reverse',
  }}
>
  {/*Put the scroll bar always on the bottom*/}
  <InfiniteScroll
    dataLength={this.state.items.length}
    next={this.fetchMoreData}
    style={{ display: 'flex', flexDirection: 'column-reverse' }} //To put endMessage and loader to the top.
    inverse={true} //
    hasMore={true}
    loader={<h4>Loading...</h4>}
    scrollableTarget="scrollableDiv"
  >
    {this.state.items.map((_, index) => (
      <div style={style} key={index}>
        div - #{index}
      </div>
    ))}
  </InfiniteScroll>
</div>

The InfiniteScroll component can be used in three ways.

  • Specify a value for the height prop if you want your scrollable content to have a specific height, providing scrollbars for scrolling your content and fetching more data.
  • If your scrollable content is being rendered within a parent element that is already providing overflow scrollbars, you can set the scrollableTarget prop to reference the DOM element and use it's scrollbars for fetching more data.
  • Without setting either the height or scrollableTarget props, the scroll will happen at document.body like Facebook's timeline scroll.

docs version wise

3.0.2

live examples

  • infinite scroll (never ending) example using react (body/window scroll)
    • Edit yk7637p62z
  • infinte scroll till 500 elements (body/window scroll)
    • Edit 439v8rmqm0
  • infinite scroll in an element (div of height 400px)
    • Edit w3w89k7x8
  • infinite scroll with scrollableTarget (a parent element which is scrollable)
    • Edit r7rp40n0zm

props

name type description
next function a function which must be called after reaching the bottom. It must trigger some sort of action which fetches the next data. The data is passed as children to the InfiniteScroll component and the data should contain previous items too. e.g. Initial data = [1, 2, 3] and then next load of data should be [1, 2, 3, 4, 5, 6].
hasMore boolean it tells the InfiniteScroll component on whether to call next function on reaching the bottom and shows an endMessage to the user
children node (list) the data items which you need to scroll.
dataLength number set the length of the data.This will unlock the subsequent calls to next.
loader node you can send a loader component to show while the component waits for the next load of data. e.g. <h3>Loading...</h3> or any fancy loader element
scrollThreshold number | string A threshold value defining when InfiniteScroll will call next. Default value is 0.8. It means the next will be called when user comes below 80% of the total height. If you pass threshold in pixels (scrollThreshold="200px"), next will be called once you scroll at least (100% - scrollThreshold) pixels down.
onScroll function a function that will listen to the scroll event on the scrolling container. Note that the scroll event is throttled, so you may not receive as many events as you would expect.
endMessage node this message is shown to the user when he has seen all the records which means he's at the bottom and hasMore is false
className string add any custom class you want
style object any style which you want to override
height number optional, give only if you want to have a fixed height scrolling content
scrollableTarget node or string optional, reference to a (parent) DOM element that is already providing overflow scrollbars to the InfiniteScroll component. You should provide the id of the DOM node preferably.
hasChildren bool children is by default assumed to be of type array and it's length is used to determine if loader needs to be shown or not, if your children is not an array, specify this prop to tell if your items are 0 or more.
pullDownToRefresh bool to enable Pull Down to Refresh feature
pullDownToRefreshContent node any JSX that you want to show the user, default={<h3>Pull down to refresh</h3>}
releaseToRefreshContent node any JSX that you want to show the user, default={<h3>Release to refresh</h3>}
pullDownToRefreshThreshold number minimum distance the user needs to pull down to trigger the refresh, default=100px , a lower value may be needed to trigger the refresh depending your users browser.
refreshFunction function this function will be called, it should return the fresh data that you want to show the user
initialScrollY number set a scroll y position for the component to render with.
inverse bool set infinite scroll on top

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Ankeet Maini

💬 📖 💻 👀 🚧

Darsh Shah

🚇

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

LICENSE

MIT

More Repositories

1

collaborative-text-editor

JavaScript
21
star
2

svelte-dates

HTML
16
star
3

food-express

https://food-express.herokuapp.com/
JavaScript
13
star
4

rn-animation-circular-progress

TypeScript
13
star
5

multi-app-single-repo-example

JavaScript
9
star
6

docker-react-nginx

JavaScript
8
star
7

awesome-front-end

A curated list of all things we love in Front End!
5
star
8

react-chunking-with-webpack

A sample to show chunking with React, React Router and of course Webpack 2
JavaScript
5
star
9

learn-webpack

A simple app to learn Webpack like a Boss!
JavaScript
4
star
10

foam-digital-garden

A ready to clone foam digital garden starter. Automatically deploys to Github Pages
JavaScript
4
star
11

aplus

JavaScript
3
star
12

react-app-redux-ts

On how to type your actions, components, reducers and store with TypeScript
TypeScript
3
star
13

css

A quick-read reference on CSS
3
star
14

simple-forms-react

An awesome Form for React!
JavaScript
3
star
15

infinite-scroll-ssr

JavaScript
2
star
16

blog

https://ankeetmaini.dev/
Nunjucks
2
star
17

rn-animation-text

TypeScript
2
star
18

react-hn

react-hn
JavaScript
2
star
19

frontend-with-react-workshop

JavaScript
2
star
20

grpc-java-service

JavaScript
1
star
21

Dogstagram

TypeScript
1
star
22

xstate-frontend-masters-workshop

JavaScript
1
star
23

rabbit-trap

TypeScript
1
star
24

react-redux-app-upto-date

A small app to keep upto speed with dependencies.
JavaScript
1
star
25

framer-motion

HTML
1
star
26

rn-animation-chrome-ios

TypeScript
1
star
27

ActivityTracker

TypeScript
1
star
28

mandatory-reviews

Mandatory reviews from groups
TypeScript
1
star
29

snake-game

TypeScript
1
star
30

modern-lens-ui

JavaScript
1
star
31

my-reddit

A simple project to learn ember-cli. It fetches reddits and does nothing fancy.
JavaScript
1
star
32

simplified-redux

JavaScript
1
star
33

my-broccoli

A simple app to learn BroccoliJS
JavaScript
1
star
34

react-native-animations

JavaScript
1
star
35

i18n-deck

1
star
36

js-scratch-pad

TypeScript
1
star
37

advanced-react-component-patterns-talk

JavaScript
1
star
38

svelte-explore

HTML
1
star
39

test-review

hi
1
star
40

rn-animation-cards

TypeScript
1
star
41

boring-mountain

Created with CodeSandbox
1
star
42

jacoco-e2e

Java
1
star
43

calver

JavaScript
1
star
44

rn-animation-tap-reanimated

TypeScript
1
star
45

rn-animation-chrome-android

TypeScript
1
star
46

css-challenges

A local codepen
JavaScript
1
star
47

on-the-fly-css-addition

JavaScript
1
star
48

photo-viewer

Simple photo viewer
TypeScript
1
star
49

animated-talk

TypeScript
1
star
50

throttled-loaders

Created with CodeSandbox
JavaScript
1
star
51

hello-github-actions

1
star
52

morph

Re-creating Ramjet from the first version solely for the purpose of learning Rich Harris's craft!
TypeScript
1
star
53

backend-garden

JavaScript
1
star
54

fawkes

A design system
TypeScript
1
star