0
0
React Nativemobile~20 mins

Pull-to-refresh patterns in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pull-to-Refresh Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this pull-to-refresh code?

Consider this React Native code snippet using RefreshControl. What will the user see when pulling down the list?

React Native
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} />}
    />
  );
}
AThe list shows 'Apple', 'Banana', and after pull-to-refresh, 'Cherry' is added to the list.
BThe list shows only 'Apple' and 'Banana' and does not update after pull-to-refresh.
CThe app crashes because RefreshControl requires a ScrollView, not FlatList.
DThe pull-to-refresh indicator never stops spinning because refreshing is never set to false.
Attempts:
2 left
💡 Hint

Look at how refreshing state is set to false after 1 second.

lifecycle
intermediate
1:30remaining
What happens if you forget to set refreshing to false?

In a React Native pull-to-refresh pattern, what is the effect of never setting the refreshing state back to false after refresh?

React Native
const onRefresh = () => {
  setRefreshing(true);
  setTimeout(() => {
    // forgot to setRefreshing(false)
  }, 1000);
};
AThe pull-to-refresh spinner keeps spinning indefinitely, blocking user interaction.
BThe spinner disappears automatically after 1 second even if refreshing stays true.
CThe app crashes with a runtime error about state updates.
DThe list refreshes normally but the spinner never shows.
Attempts:
2 left
💡 Hint

Think about what controls the spinner visibility.

navigation
advanced
2:30remaining
How to preserve pull-to-refresh state when navigating back?

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?

ARemove the pull-to-refresh feature to avoid state loss.
BStore the refreshed data in a global state or context so it persists across navigation.
CCall <code>setRefreshing(true)</code> in <code>useEffect</code> without fetching data again.
DUse <code>setTimeout</code> to delay navigation so data stays refreshed.
Attempts:
2 left
💡 Hint

Think about where the data lives and how navigation affects component state.

📝 Syntax
advanced
2:30remaining
Which option correctly implements pull-to-refresh with functional component?

Choose the code snippet that correctly implements pull-to-refresh in React Native functional component.

A
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () =&gt; {
  setRefreshing(true);
  fetchData().then(() =&gt; setRefreshing(false));
};
&lt;FlatList refreshControl={&lt;RefreshControl refreshing={refreshing} onRefresh={onRefresh} /&gt;} /&gt;
B
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () =&gt; {
  fetchData().then(() =&gt; setRefreshing(false));
};
&lt;FlatList refreshControl={&lt;RefreshControl refreshing={refreshing} onRefresh={onRefresh} /&gt;} /&gt;
C
const [refreshing, setRefreshing] = useState(false);
const onRefresh = async () =&gt; {
  setRefreshing(true);
  await fetchData();
  setRefreshing(false);
};
&lt;FlatList refreshControl={&lt;RefreshControl refreshing={refreshing} onRefresh={onRefresh} /&gt;} /&gt;
D
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () =&gt; {
  setRefreshing(true);
  fetchData();
  setRefreshing(false);
};
&lt;FlatList refreshControl={&lt;RefreshControl refreshing={refreshing} onRefresh={onRefresh} /&gt;} /&gt;
Attempts:
2 left
💡 Hint

Remember to set refreshing true before fetching and false after fetch completes.

🔧 Debug
expert
3:00remaining
Why does pull-to-refresh not trigger on Android?

This React Native app uses RefreshControl inside a ScrollView. Pull-to-refresh works on iOS but not on Android. What is the cause?

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), 1000);
  };

  return (
    <ScrollView style={{flex:1}} refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}>
      <Text>Pull down to refresh</Text>
    </ScrollView>
  );
}
AAndroid requires an extra prop <code>enabled={true}</code> on RefreshControl.
BRefreshControl is not supported on Android ScrollView, only on FlatList.
CThe refreshing state must be initialized to true on Android for pull-to-refresh to work.
DAndroid requires the ScrollView to have a fixed height or flex style to enable pull-to-refresh.
Attempts:
2 left
💡 Hint

Check the container style and how Android handles ScrollView layout.