Consider this React Native component that fetches data and shows a loading spinner while waiting. What will the user see on the screen before the data loads?
import React, { useState, useEffect } from 'react'; import { View, Text, ActivityIndicator } from 'react-native'; export default function DataLoader() { const [loading, setLoading] = useState(true); const [data, setData] = useState(null); useEffect(() => { setTimeout(() => { setData('Hello!'); setLoading(false); }, 3000); }, []); if (loading) { return <ActivityIndicator accessibilityLabel="Loading indicator" size="large" />; } return <Text>{data}</Text>; }
Look at the loading state and what the component returns when loading is true.
The component starts with loading = true, so it returns the ActivityIndicator spinner. After 3 seconds, loading becomes false and data is set, so the text 'Hello!' is shown.
This React Native component tries to fetch data but may fail. Which option correctly displays an error message if the fetch fails?
import React, { useState, useEffect } from 'react'; import { View, Text, ActivityIndicator } from 'react-native'; export default function FetchWithError() { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [data, setData] = useState(null); useEffect(() => { setTimeout(() => { setError('Network error'); setLoading(false); }, 2000); }, []); if (loading) { return <ActivityIndicator size="large" />; } if (error) { return <Text accessibilityRole="alert">Error: {error}</Text>; } return <Text>{data}</Text>; }
Check the order of the if statements and when loading and error change.
The component shows a spinner while loading is true. After 2 seconds, loading becomes false and error is set, so it shows the error message.
In this React Native component, what issue can occur if the component unmounts before the data finishes loading?
import React, { useState, useEffect } from 'react'; import { Text } from 'react-native'; export default function UnmountIssue() { const [data, setData] = useState(null); useEffect(() => { const timer = setTimeout(() => { setData('Loaded'); }, 5000); return () => clearTimeout(timer); }, []); if (!data) { return <Text>Loading...</Text>; } return <Text>{data}</Text>; }
Look at the cleanup function returned by useEffect.
The cleanup function clears the timer when the component unmounts, preventing setData from running after unmount and avoiding memory leaks.
What error will this React Native component produce when it tries to render?
import React, { useState, useEffect } from 'react'; import { Text } from 'react-native'; export default function MissingLoading() { const [data, setData] = useState(null); useEffect(() => { setTimeout(() => { setData('Done'); }, 1000); }, []); if (loading) { return <Text>Loading...</Text>; } return <Text>{data}</Text>; }
Check if loading is declared anywhere in the component.
The variable loading is used but never declared, causing a ReferenceError at runtime.
Choose the best explanation for why mobile apps should show clear loading and error states to users.
Think about how users feel when waiting or when something goes wrong.
Showing loading and error states helps users understand what is happening, reducing frustration and improving trust in the app.