Component - Pull-to-refresh
This UI component allows users to refresh the content of a list by pulling down on the screen. It is a common pattern in mobile apps to update data easily.
This UI component allows users to refresh the content of a list by pulling down on the screen. It is a common pattern in mobile apps to update data easily.
View
└── ScrollView (with RefreshControl)
└── Content (e.g., Text items)RefreshControl in a React Native app?RefreshControl is a component used inside scrollable views to provide pull-to-refresh behavior.RefreshControl inside a ScrollView?refreshControl prop expects a RefreshControl component with props refreshing and onRefresh.refreshing and onRefresh inside <RefreshControl /> as a JSX element.const [refreshing, setRefreshing] = React.useState(false);
const onRefresh = React.useCallback(() => {
setRefreshing(true);
setTimeout(() => setRefreshing(false), 2000);
}, []);
return (
<ScrollView refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}>
<Text>Pull down to refresh</Text>
</ScrollView>
);onRefresh sets refreshing to true, showing the spinner.setRefreshing(false) hides the spinner automatically.const [refreshing, setRefreshing] = React.useState(false);
function onRefresh() {
setRefreshing(true);
fetchData();
setRefreshing(false);
}
return (
<FlatList
data={data}
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}
renderItem={({item}) => <Text>{item}</Text>}
/>
);setRefreshing(false) immediately after fetchData(), without waiting for data to load.SectionList that fetches new data asynchronously. Which approach correctly ensures the spinner shows during fetch and hides after completion?
const [refreshing, setRefreshing] = React.useState(false);
const onRefresh = async () => {
// What goes here?
};setRefreshing(true) to show spinner before starting data fetch.await fetchData() to wait for data, then call setRefreshing(false) to hide spinner after completion.Pass the test and this pattern moves unknown