64 | );
65 | };
66 |
67 | export default InfiniteList;
68 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-infinite-list
2 |
3 | A handy React component to render infinite, onScroll fetched, data lists.
4 |
5 | Demo: https://react-infinite-list.netlify.com
6 |
7 | 
8 | 
9 |
10 | ---
11 |
12 | ### Install
13 |
14 | ```sh
15 | $ npm install @damnhotuser/react-infinite-list
16 | ```
17 |
18 | ---
19 |
20 | ### Usage
21 |
22 | `react-infinite-list` provides a single component, `InfiniteList`.
23 |
24 | ```jsx
25 | import React, { useState } from "react";
26 |
27 | import InfiniteList from "@damnhotuser/react-infinite-list";
28 | import mock_fetch from "./mock_data"; // mocking an API service
29 |
30 | const App = props => {
31 | const [rows, setRows] = useState([]);
32 |
33 | const fetchData = (offset, limit) =>
34 | mock_fetch(offset, limit).then(data => {
35 | setRows([...rows, ...data]);
36 | });
37 |
38 | return (
39 | /*
40 | * InfiniteList takes three needed arguments:
41 | * `rows` are data to display in the list
42 | * `fetchData` is called on rendering and when needed, on scroll
43 | * `limit` is the number of rows to fetch on each `fetchData` call
44 | *
45 | * InfiniteList's `children` must be a function, and has the row to render passed as an argument
46 | *
47 | * current `index` and `ref` will also be passed as arguments of your `children` function, it is IMPORTANT to pass ref to the rendered list element for the onScroll fetch to work
48 | */
49 |
50 | {(row, index, ref) => (
51 |
52 | {row.name}
53 |
54 | )}
55 |
56 | );
57 | };
58 | ```
59 |
60 |
61 |
62 | **⚠️ InfiniteList's `children` must be a function. The current row to render will be passed as an argument.**
63 |
64 | **⚠️ current `index` and `ref` will also be passed as arguments of your `children` function, it is IMPORTANT to pass ref to the rendered list element for the onScroll fetch to work**
65 |
66 |
67 |
68 | `InfiniteList` takes also 3 optionnal arguments:
69 |
70 | - `containerClasses` are classes that will be passed as an argument to the `div` holding your list.
71 | - `containerStyle` are style rules that will be passed as an argument to the `div` holding your list.
72 | - `fetchThreshold` (number) is the number of element before the end of the displayed list to wait before calling `fetchData` again. i.e. if n elements are displayed on the list, and `fetchThreshold` is set to 3,`fetchData` will be called when the n-3th element of the list is scrolled into view. **The default value of `fetchthreshold` is 5**.
73 |
--------------------------------------------------------------------------------