|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | +import { SectionList, StyleSheet, Text } from 'react-native'; |
| 3 | +import { ListItem } from '../src/components'; |
| 4 | +import { useList } from 'react-native-use-list'; |
| 5 | + |
| 6 | +const DATA = [ |
| 7 | + { |
| 8 | + title: 'Main dishes', |
| 9 | + data: ['Pizza', 'Burger', 'Risotto'], |
| 10 | + }, |
| 11 | + { |
| 12 | + title: 'Sides', |
| 13 | + data: ['French Fries', 'Onion Rings', 'Fried Shrimps'], |
| 14 | + }, |
| 15 | + { |
| 16 | + title: 'Drinks', |
| 17 | + data: ['Water', 'Coke', 'Beer'], |
| 18 | + }, |
| 19 | + { |
| 20 | + title: 'Desserts', |
| 21 | + data: ['Cheese Cake', 'Ice Cream'], |
| 22 | + }, |
| 23 | +]; |
| 24 | + |
| 25 | +export const SectionListPullToRefreshExample = () => { |
| 26 | + const [data, setData] = useState(DATA.slice(0, 2)); |
| 27 | + |
| 28 | + const listRef = useRef(null); |
| 29 | + |
| 30 | + const updateData = () => { |
| 31 | + return new Promise((resolve) => { |
| 32 | + setTimeout(() => { |
| 33 | + setData(DATA); |
| 34 | + resolve(true); |
| 35 | + }, 1000 * 2); |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + const { isRefreshing, refreshController } = useList(listRef, { |
| 40 | + onRefresh: updateData, |
| 41 | + }); |
| 42 | + |
| 43 | + return ( |
| 44 | + <> |
| 45 | + <Text |
| 46 | + style={styles.title} |
| 47 | + >{`isRefreshing: ${isRefreshing} - Length: ${data.length}`}</Text> |
| 48 | + <SectionList |
| 49 | + ref={listRef} |
| 50 | + sections={data} |
| 51 | + keyExtractor={(item, index) => item + index} |
| 52 | + renderItem={({ item }) => <ListItem text={item} />} |
| 53 | + renderSectionHeader={({ section }) => ( |
| 54 | + <Text style={styles.header}>{section.title}</Text> |
| 55 | + )} |
| 56 | + {...refreshController} |
| 57 | + /> |
| 58 | + </> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +const styles = StyleSheet.create({ |
| 63 | + header: { |
| 64 | + fontSize: 32, |
| 65 | + backgroundColor: '#fff', |
| 66 | + }, |
| 67 | + title: { |
| 68 | + fontSize: 24, |
| 69 | + textAlign: 'center', |
| 70 | + marginVertical: 8, |
| 71 | + }, |
| 72 | +}); |
0 commit comments