Challenge - 5 Problems
Pull-to-refresh Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you pull down on a ScrollView with RefreshControl?
In React Native, when you add a
RefreshControl to a ScrollView, what is the expected behavior when the user pulls down on the list?React Native
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), 2000); }; return ( <ScrollView refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />} > <Text>Pull down to refresh</Text> </ScrollView> ); }
Attempts:
2 left
💡 Hint
Think about what the RefreshControl component does when refreshing is true.
✗ Incorrect
When you pull down on a ScrollView with RefreshControl, it triggers the onRefresh callback and shows a spinner until the refreshing state is set to false.
❓ lifecycle
intermediate2:00remaining
What is the role of the refreshing state in Pull-to-refresh?
In a React Native pull-to-refresh implementation, why do we need a
refreshing state variable?React Native
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () => {
setRefreshing(true);
fetchData().then(() => setRefreshing(false));
};Attempts:
2 left
💡 Hint
Think about what the RefreshControl uses to show or hide the loading spinner.
✗ Incorrect
The refreshing state tells RefreshControl when to show or hide the spinner. Setting it to true shows the spinner, and false hides it after data loads.
📝 Syntax
advanced2:30remaining
Which code snippet correctly implements pull-to-refresh in a FlatList?
Select the code snippet that correctly uses
RefreshControl with a FlatList to enable pull-to-refresh.Attempts:
2 left
💡 Hint
Remember that RefreshControl is a component and must be passed as JSX inside refreshControl prop.
✗ Incorrect
Option A correctly passes a RefreshControl component with refreshing and onRefresh props. Option A uses props that FlatList does not support directly. Option A calls RefreshControl like a function, which is invalid. Option A misses the refreshing prop, so spinner won't show.
🔧 Debug
advanced2:30remaining
Why does the spinner never stop after pull-to-refresh?
Given this code snippet, why does the spinner keep spinning forever after pulling to refresh?
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () => {
setRefreshing(true);
fetchData();
};
React Native
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () => {
setRefreshing(true);
fetchData();
};Attempts:
2 left
💡 Hint
Think about when the refreshing state is set back to false.
✗ Incorrect
The spinner stays because refreshing remains true. The code never sets refreshing to false after data loads, so the spinner never hides.
🧠 Conceptual
expert3:00remaining
How to ensure accessibility for pull-to-refresh in React Native?
Which approach best improves accessibility for users relying on screen readers when implementing pull-to-refresh in React Native?
Attempts:
2 left
💡 Hint
Think about how screen readers get notified about UI changes.
✗ Incorrect
Adding accessible labels and using accessibilityLiveRegion helps screen readers announce when refresh starts and ends, improving accessibility.