Skip to content

Commit a9ccb06

Browse files
nitkukankeetmaini
authored andcommitted
Fixes multiple api call on scroll up down and adds data length prop (ankeetmaini#52)
* Fixes multiple api call on scroll up down and adds data length prop * Updates the readme * Corrects sentence * Changes example
1 parent 92b99e5 commit a9ccb06

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ added. An infinite-scroll that actually works and super-simple to integrate!
2222
```jsx
2323
<InfiniteScroll
2424
pullDownToRefresh
25+
dataLength={items.length} //This is important field to render the next data
2526
pullDownToRefreshContent={
2627
<h3 style={{textAlign: 'center'}}>&#8595; Pull down to refresh</h3>
2728
}
@@ -48,12 +49,17 @@ The `InfiniteScroll` component can be used in three ways.
4849
- Without setting either the `height` or `scrollableTarget` props, the scroll will happen at `document.body` like *Facebook's* timeline scroll.
4950

5051

52+
# docs version wise
53+
[3.0.2](docs/README-3.0.2.md)
54+
55+
5156
# props
5257
name | type | description
5358
-----|------|------------
5459
**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]*.
5560
**hasMore** | boolean | it tells the `InfiniteScroll` component on whether to call `next` function on reaching the bottom and shows an `endMessage` to the user
5661
**children** | node (list) | the data items which you need to scroll.
62+
**dataLength** | number | set the length of the data.This will unlock the subsequent calls to next.
5763
**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
5864
**scrollThreshold** | number | a threshold value after that the `InfiniteScroll` will call `next`. By default it's `0.8`. It means the `next` will be called when the user comes below 80% of the total height.
5965
**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.

app/index.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ export default class InfiniteScroll extends Component {
7575
}
7676

7777
componentWillReceiveProps (props) {
78-
// new data was sent in
78+
79+
// do nothing when dataLength is unchanged
80+
if (this.props.dataLength === props.dataLength) return;
81+
82+
// update state when new data was sent in
7983
this.setState({
8084
showLoader: false,
8185
actionTriggered: false,
@@ -150,15 +154,6 @@ export default class InfiniteScroll extends Component {
150154
? event.target
151155
: (document.documentElement.scrollTop ? document.documentElement : document.body);
152156

153-
// if user scrolls up, remove action trigger lock
154-
if (target.scrollTop < this.state.lastScrollTop) {
155-
this.setState({
156-
actionTriggered: false,
157-
lastScrollTop: target.scrollTop
158-
});
159-
return; // user's going up, we don't care
160-
}
161-
162157
// return immediately if the action has already been triggered,
163158
// prevents multiple triggers.
164159
if (this.state.actionTriggered) return;
@@ -214,7 +209,7 @@ export default class InfiniteScroll extends Component {
214209
{this.props.children}
215210
{!this.state.showLoader && !hasChildren && this.props.hasMore &&
216211
this.props.loader}
217-
{this.state.showLoader && this.props.loader}
212+
{this.state.showLoader && this.props.hasMore && this.props.loader}
218213
{!this.props.hasMore && this.props.endMessage}
219214
</div>
220215
</div>
@@ -246,4 +241,5 @@ InfiniteScroll.propTypes = {
246241
pullDownToRefreshThreshold: PropTypes.number,
247242
refreshFunction: PropTypes.func,
248243
onScroll: PropTypes.func,
244+
dataLength: PropTypes.number.isRequired,
249245
};

docs/README-3.0.2.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# react-infinite-scroll-component [![npm](https://img.shields.io/npm/dt/react-infinite-scroll-component.svg?style=flat-square)](https://www.npmjs.com/package/react-infinite-scroll-component) [![npm](https://img.shields.io/npm/v/react-infinite-scroll-component.svg?style=flat-square)](https://www.npmjs.com/package/react-infinite-scroll-component)
2+
3+
A component to make all your infinite scrolling woes go away with just 4.15 kB! `Pull Down to Refresh` feature
4+
added. An infinite-scroll that actually works and super-simple to integrate!
5+
6+
# install
7+
```bash
8+
npm install --save react-infinite-scroll-component
9+
10+
// in code ES6
11+
import InfiniteScroll from 'react-infinite-scroll-component';
12+
// or commonjs
13+
var InfiniteScroll = require('react-infinite-scroll-component');
14+
```
15+
16+
# demos
17+
- [See the demo in action at http://ankeetmaini.github.io/react-infinite-scroll-component/](http://ankeetmaini.github.io/react-infinite-scroll-component/). Thanks [@kdenz](https://github.com/kdenz)!
18+
- The code for demos is in the `demos/` directory. You can also clone and open `lib/index.html` in your browser to see the demos in action.
19+
20+
# using
21+
22+
```jsx
23+
<InfiniteScroll
24+
pullDownToRefresh
25+
pullDownToRefreshContent={
26+
<h3 style={{textAlign: 'center'}}>&#8595; Pull down to refresh</h3>
27+
}
28+
releaseToRefreshContent={
29+
<h3 style={{textAlign: 'center'}}>&#8593; Release to refresh</h3>
30+
}
31+
refreshFunction={this.refresh}
32+
next={fetchData}
33+
hasMore={true}
34+
loader={<h4>Loading...</h4>}
35+
endMessage={
36+
<p style={{textAlign: 'center'}}>
37+
<b>Yay! You have seen it all</b>
38+
</p>
39+
}>
40+
{items}
41+
</InfiniteScroll>
42+
```
43+
44+
The `InfiniteScroll` component can be used in three ways.
45+
46+
- 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.
47+
- 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.
48+
- Without setting either the `height` or `scrollableTarget` props, the scroll will happen at `document.body` like *Facebook's* timeline scroll.
49+
50+
51+
# props
52+
name | type | description
53+
-----|------|------------
54+
**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]*.
55+
**hasMore** | boolean | it tells the `InfiniteScroll` component on whether to call `next` function on reaching the bottom and shows an `endMessage` to the user
56+
**children** | node (list) | the data items which you need to scroll.
57+
**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
58+
**scrollThreshold** | number | a threshold value after that the `InfiniteScroll` will call `next`. By default it's `0.8`. It means the `next` will be called when the user comes below 80% of the total height.
59+
**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.
60+
**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`
61+
**style** | object | any style which you want to override
62+
**height** | number | optional, give only if you want to have a fixed height scrolling content
63+
**scrollableTarget** | node | optional, reference to a (parent) DOM element that is already providing overflow scrollbars to the `InfiniteScroll` component.
64+
**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.
65+
**pullDownToRefresh** | bool | to enable **Pull Down to Refresh** feature
66+
**pullDownToRefreshContent** | node | any JSX that you want to show the user, `default={<h3>Pull down to refresh</h3>}`
67+
**releaseToRefreshContent** | node | any JSX that you want to show the user, `default={<h3>Release to refresh</h3>}`
68+
**pullDownToRefreshThreshold** | number | minimum distance the user needs to pull down to trigger the refresh, `default=100px`
69+
**refreshFunction** | function | this function will be called, it should return the fresh data that you want to show the user
70+
**initialScrollY** | number | set a scroll y position for the component to render with.

0 commit comments

Comments
 (0)