-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInfiniteVirtualizedList.js
70 lines (62 loc) · 2.18 KB
/
InfiniteVirtualizedList.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React from 'react'
import PropTypes from 'prop-types'
import { VirtualizedList } from 'react-native'
import { InfiniteLoader } from 'react-virtualized/dist/commonjs/InfiniteLoader'
const noop = () => {}
export default class InfiniteVirtualizedList extends React.Component {
static propTypes = {
hasNextPage: PropTypes.bool.isRequired,
isNextPageLoading: PropTypes.bool.isRequired,
loadNextPage: PropTypes.func.isRequired,
renderItem: PropTypes.func.isRequired,
paginationWaitingView: PropTypes.func,
data: PropTypes.arrayOf(PropTypes.shape()).isRequired,
}
static defaultProps = {
paginationWaitingView: noop,
}
render() {
const { hasNextPage, isNextPageLoading, data, loadNextPage, renderItem, paginationWaitingView } = this.props
// If there are more items to be loaded then add an extra row to hold a loading indicator.
const rowCount = hasNextPage
? data.length + 1
: data.length
// Only load 1 page of items at a time.
// Pass an empty callback to InfiniteLoader in case it asks us to load more than once.
const loadMoreRows = isNextPageLoading
? noop
: loadNextPage
// Every row is loaded except for our loading indicator row.
const isRowLoaded = ({ index }) => !hasNextPage || index < data.length
// Render a list item or a loading indicator.
const rowRenderer = ({ item, index }) => {
if (!isRowLoaded({ index })) {
return paginationWaitingView()
}
return renderItem({ item, index })
}
return (
<InfiniteLoader
isRowLoaded={isRowLoaded}
loadMoreRows={loadMoreRows}
rowCount={rowCount}
>
{({ onRowsRendered, registerChild }) => (
<VirtualizedList
onViewableItemsChanged={({ viewableItems }) => {
if (viewableItems.length > 0) {
onRowsRendered({
startIndex: viewableItems[0].index,
stopIndex: viewableItems[viewableItems.length - 1].index,
})
}
}}
ref={registerChild}
renderItem={rowRenderer}
{...this.props}
/>
)}
</InfiniteLoader>
)
}
}