Skip to content

Commit 2ac9ff7

Browse files
authored
Update README.md
1 parent 3d6a225 commit 2ac9ff7

File tree

1 file changed

+70
-32
lines changed

1 file changed

+70
-32
lines changed

README.md

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,95 @@ If you want to use the older version which is using `setInterval`, you can find
2424
npm install react-infinite-scroll-hook
2525
```
2626

27-
### Basic Usage
27+
### Simple Example
2828

2929
```javascript
3030
import useInfiniteScroll from 'react-infinite-scroll-hook';
3131

32-
function InfiniteList({}) {
33-
const [items, setItems] = useState([]);
34-
const [hasNextPage, setHasNextPage] = useState();
35-
const [loading, setLoading] = useState(false);
32+
function SimpleInfiniteList() {
33+
const { loading, items, hasNextPage, error, loadMore } = useLoadItems();
3634

37-
38-
/// ...
39-
40-
function handleLoadMore() {
41-
setLoading(true);
42-
// Some API call to fetch the next page
43-
loadNextPage(endCursor, pageSize).then((newPage) => {
44-
setLoading(false);
45-
setHasNextPage(newPage.hasNextPage);
46-
setItems([...items, newPage.items]);
47-
});
48-
}
49-
50-
const infiniteRef = useInfiniteScroll({
35+
const [infiniteRef] = useInfiniteScroll({
5136
loading,
5237
hasNextPage,
53-
onLoadMore: handleLoadMore,
54-
scrollContainer,
38+
onLoadMore: loadMore,
39+
// When there is an error, we stop infinite loading.
40+
// It can be reactivated by setting "error" state as undefined.
41+
disabled: !!error,
42+
// `rootMargin` is passed to `IntersectionObserver`.
43+
// We can use it to trigger 'onLoadMore' when the sentry comes near to become
44+
// visible, instead of becoming fully visible on the screen.
45+
rootMargin: '0px 0px 400px 0px',
5546
});
5647

57-
// ...
58-
5948
return (
60-
<List ref={infiniteRef}>
49+
<List>
6150
{items.map((item) => (
6251
<ListItem key={item.key}>{item.value}</ListItem>
6352
))}
64-
{loading && <ListItem>Loading...</ListItem>}
53+
{/*
54+
As long as we have a "next page", we show "Loading" right under the list.
55+
When it becomes visible on the screen, or it comes near, it triggers 'onLoadMore'.
56+
This is our "sentry".
57+
We can also use another "sentry" which is separated from the "Loading" component like:
58+
<div ref={infiniteRef} />
59+
{loading && <ListItem>Loading...</ListItem>}
60+
and leave "Loading" without this ref.
61+
*/}
62+
{hasNextPage && (
63+
<ListItem ref={infiniteRef}>
64+
<Loading />
65+
</ListItem>
66+
)}
6567
</List>
6668
);
6769
}
6870
```
6971

70-
### Props
72+
Or if we have a scrollable container and we want to use it as our "list container" instead of `document`, we just need to use `rootRef` like:
73+
```js
74+
function InfiniteListWithVerticalScroll() {
75+
const { loading, items, hasNextPage, error, loadMore } = useLoadItems();
76+
77+
const [infiniteRef, { rootRef }] = useInfiniteScroll({
78+
loading,
79+
hasNextPage,
80+
onLoadMore: loadMore,
81+
disabled: !!error,
82+
rootMargin: '0px 0px 400px 0px',
83+
});
84+
85+
return (
86+
<ListContainer
87+
// This where we set our scrollable root component.
88+
ref={rootRef}
89+
>
90+
<List>
91+
{items.map((item) => (
92+
<ListItem key={item.key}>{item.value}</ListItem>
93+
))}
94+
{hasNextPage && (
95+
<ListItem ref={infiniteRef}>
96+
<Loading />
97+
</ListItem>
98+
)}
99+
</List>
100+
</ListContainer>
101+
);
102+
}
103+
```
71104

72-
- **loading:** Some sort of "fetching" info of the request.
73-
- **hasNextPage:** If the list has more items to load.
74-
- **onLoadMore:** The callback function to execute when the threshold is exceeded.
75-
- **threshold:** Maximum distance to bottom of the window/parent to trigger the callback. Default is 150.
76-
- **checkInterval:** Frequency to check the dom. Default is 200.
77-
- **scrollContainer:** May be `"window"` or `"parent"`. Default is `"window"`. If you want to use a scrollable parent for the infinite list, use `"parent"`.
105+
You can find different layout examples **[here](https://github.com/onderonur/react-infinite-scroll-hook/tree/master/example/examples)**. **[Live demo](https://onderonur.github.io/react-infinite-scroll-hook/)** contains all of these cases.
106+
107+
## Arguments
108+
| Name | Description | Type | Optional | Default Value |
109+
| ----------- | ----------- | ---- | -------- | ------------- |
110+
| loading | Some sort of "is fetching" info of the request. | boolean || |
111+
| hasNextPage | If the list has more items to load. | boolean || |
112+
| onLoadMore | The callback function to execute when the 'onLoadMore' is triggered. | VoidFunction || |
113+
| rootMargin | We pass this to 'IntersectionObserver'. We can use it to configure when to trigger 'onLoadMore'. | string || |
114+
| disabled | Flag to stop infinite scrolling. Can be used in case of an error etc too. | boolean || |
115+
| delayInMs | How long it should wait before triggering 'onLoadMore'. | number || 100 |
78116

79117
[build-badge]: https://img.shields.io/travis/user/repo/master.png?style=flat-square
80118
[build]: https://travis-ci.org/user/repo

0 commit comments

Comments
 (0)