-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPullToRefresh.tsx
57 lines (49 loc) · 1.38 KB
/
PullToRefresh.tsx
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
import React, { useState } from 'react';
import { VirtualizedList, StyleSheet, Text } from 'react-native';
import { ListItem } from '../../src/components';
import { generateRandomColor } from '../../src/utils/colors';
import { useList } from 'react-native-use-list';
const list1 = generateRandomColor(5);
const list2 = generateRandomColor(10);
export const VirtualizedListPullToRefreshExample = () => {
const [data, setData] = useState(list1);
const getItem = (_x: any, index: number) => data[index] || '';
const getItemCount = () => data.length;
const updateData = () => {
return new Promise((resolve) => {
setTimeout(() => {
setData(list2);
resolve(true);
}, 1000 * 2);
});
};
const { isRefreshing, refreshController } = useList({
onRefresh: updateData,
});
return (
<>
<Text
style={styles.title}
>{`isRefreshing: ${isRefreshing} - Length: ${data.length}`}</Text>
<VirtualizedList
initialNumToRender={4}
renderItem={({ index }) => <ListItem text={index} />}
keyExtractor={(item: string) => item}
getItemCount={getItemCount}
getItem={getItem}
style={styles.list}
{...refreshController}
/>
</>
);
};
const styles = StyleSheet.create({
list: {
flexGrow: 0,
},
title: {
fontSize: 24,
textAlign: 'center',
marginVertical: 8,
},
});