import React, { useState, useCallback } from 'react';
import { SafeAreaView, FlatList, Text, RefreshControl } from 'react-native';
export default function PullToRefreshScreen() {
const [items, setItems] = useState([
'Item 1',
'Item 2',
'Item 3',
'Item 4',
'Item 5'
]);
const [refreshing, setRefreshing] = useState(false);
const onRefresh = useCallback(() => {
setRefreshing(true);
setTimeout(() => {
setItems((prevItems) => prevItems.map(item => item.includes(' - refreshed') ? item : item + ' - refreshed'));
setRefreshing(false);
}, 1000); // Simulate network request delay
}, []);
return (
<SafeAreaView style={{ flex: 1 }}>
<FlatList
data={items}
keyExtractor={(item) => item}
renderItem={({ item }) => <Text style={{ padding: 20 }} accessibilityLabel={item}>{item}</Text>}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
accessibilityLabel="Pull to refresh list"
/>
}
/>
</SafeAreaView>
);
}
We use React Native's RefreshControl inside the FlatList to enable pull-to-refresh. The onRefresh function sets the refreshing state to true, simulates a delay with setTimeout to mimic a network call, then updates the list items by appending ' - refreshed' only once to each item. Finally, it sets refreshing to false to hide the spinner. Accessibility labels are added to the refresh control and each item for screen readers.