Complete the code to display a loading spinner while the app is fetching data.
return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> {isLoading ? <ActivityIndicator size=[1] /> : <Text>Data Loaded</Text>} </View> );
The ActivityIndicator component's size prop accepts "small" or "large". "large" shows a bigger spinner, which is more visible during loading.
Complete the code to prevent unnecessary re-renders by using React.memo.
const ListItem = React.memo(function ListItem({ item }) {
return <Text>{item.name}</Text>;
});
export default function App() {
const [data, setData] = React.useState([]);
return <FlatList data={data} renderItem={({ item }) => <ListItem [1] />} keyExtractor={item => item.id} />;
}The ListItem component expects a prop named item. Passing item={item} ensures it receives the correct data.
Fix the error in the code to avoid blocking the UI thread during heavy computation.
function heavyCalculation() {
let sum = 0;
for (let i = 0; i < 1000000000; i++) {
sum += i;
}
return sum;
}
export default function App() {
const [result, setResult] = React.useState(null);
React.useEffect(() => {
const res = [1];
setResult(res);
}, []);
return <Text>{result}</Text>;
}Using setTimeout with 0 delay defers the heavy calculation, allowing the UI to remain responsive.
Fill both blanks to optimize image loading with React Native's FastImage component.
import FastImage from 'react-native-fast-image'; export default function App() { return ( <FastImage style={{ width: 200, height: 200 }} source={{ uri: [1], priority: [2] }} /> ); }
The uri should be a valid image URL. Setting priority to "high" tells FastImage to load the image quickly, improving user experience.
Fill all three blanks to implement a simple caching mechanism for API data using React Native's AsyncStorage.
import AsyncStorage from '@react-native-async-storage/async-storage'; async function fetchData() { const cached = await AsyncStorage.getItem([1]); if (cached) { return JSON.parse(cached); } const response = await fetch([2]); const data = await response.json(); await AsyncStorage.setItem([3], JSON.stringify(data)); return data; }
Use the same key "apiCache" to get and set cached data. The URL "https://api.example.com/data" is the API endpoint to fetch fresh data.