Consider this React Native code snippet using RefreshControl. What will the user see when pulling down the list?
import React, { useState } from 'react'; import { FlatList, RefreshControl, Text, View } from 'react-native'; export default function App() { const [refreshing, setRefreshing] = useState(false); const [items, setItems] = useState(['Apple', 'Banana']); const onRefresh = () => { setRefreshing(true); setTimeout(() => { setItems([...items, 'Cherry']); setRefreshing(false); }, 1000); }; return ( <FlatList data={items} keyExtractor={(item) => item} renderItem={({ item }) => <Text>{item}</Text>} refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />} /> ); }
Look at how refreshing state is set to false after 1 second.
The onRefresh function sets refreshing to true, then after 1 second adds 'Cherry' to the list and sets refreshing back to false. This causes the pull-to-refresh spinner to show briefly and the list to update.
In a React Native pull-to-refresh pattern, what is the effect of never setting the refreshing state back to false after refresh?
const onRefresh = () => {
setRefreshing(true);
setTimeout(() => {
// forgot to setRefreshing(false)
}, 1000);
};Think about what controls the spinner visibility.
The refreshing prop controls the spinner. If it stays true, the spinner never stops spinning, confusing the user.
You have a screen with pull-to-refresh that fetches data. When navigating away and back, the list resets and does not keep refreshed data. How to fix this?
Think about where the data lives and how navigation affects component state.
Component state resets on navigation. Storing data in a global store or context keeps it persistent, so the refreshed list remains when returning.
Choose the code snippet that correctly implements pull-to-refresh in React Native functional component.
Remember to set refreshing true before fetching and false after fetch completes.
Option C uses async/await to set refreshing true before fetch and false after fetch completes, ensuring spinner shows correctly.
This React Native app uses RefreshControl inside a ScrollView. Pull-to-refresh works on iOS but not on Android. What is the cause?
import React, { useState } from 'react'; import { ScrollView, RefreshControl, Text } from 'react-native'; export default function App() { const [refreshing, setRefreshing] = useState(false); const onRefresh = () => { setRefreshing(true); setTimeout(() => setRefreshing(false), 1000); }; return ( <ScrollView style={{flex:1}} refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}> <Text>Pull down to refresh</Text> </ScrollView> ); }
Check the container style and how Android handles ScrollView layout.
On Android, ScrollView must have a fixed height or flex style so it can detect pull gestures for RefreshControl. Without it, pull-to-refresh won't trigger.