forked from mountain-pass/react-infinite-scroll-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
45 lines (41 loc) · 1.28 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react'
import InfiniteScrollTrigger from 'react-infinite-scroll-trigger'
const App = () => {
const [data, setData] = React.useState([])
const loadMore = () => {
// mimic an async operation...
return new Promise((resolve) => {
setTimeout(() => {
// now we've loaded more data, append it to the end of the 'data' array...
setData((prevState) => {
const newData = [
...prevState,
`Element_${prevState.length + 1}`,
`Element_${prevState.length + 2}`,
`Element_${prevState.length + 3}`
]
resolve(newData.length <= 14) // keep loading data until we exceed 14 records
return newData
})
resolve()
}, 1500)
})
}
return (
<React.Fragment>
<h1>Records loaded = {data.length}</h1>
<div style={{ border: '1px solid magenta', height: '400px', overflow: 'auto' }}>
{data.map((d) => (
<div key={d} style={{ border: '1px solid blue', height: '50px' }}>
{d}
</div>
))}
<InfiniteScrollTrigger
loadMore={loadMore}
style={{ height: '130px', marginTop: '-130px', background: 'rgba(0,255,0,0.2)' }}
/>
</div>
</React.Fragment>
)
}
export default App