Loading and error states help users know what is happening in the app. They show when data is loading or if something went wrong.
Loading and error states in React Native
Introduction
Syntax
React Native
function MyComponent() {
const [loading, setLoading] = React.useState(false);
const [error, setError] = React.useState(null);
if (loading) {
return <Text>Loading...</Text>;
}
if (error) {
return <Text>Error: {error}</Text>;
}
return <Text>Data loaded successfully!</Text>;
}Use loading state to show a spinner or message while waiting.
Use error state to show a friendly message if something goes wrong.
Examples
React Native
const [loading, setLoading] = React.useState(true); if (loading) { return <Text>Loading data...</Text>; }
React Native
const [error, setError] = React.useState('Network error'); if (error) { return <Text>Error: {error}</Text>; }
React Native
if (loading) { return <ActivityIndicator size="large" color="#0000ff" />; }
Sample App
This app shows a button to load data. When pressed, it shows a spinner for 2 seconds. Then it randomly shows data or an error message.
React Native
import React from 'react'; import { View, Text, Button, ActivityIndicator } from 'react-native'; export default function LoadingErrorExample() { const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const [data, setData] = React.useState(null); const fetchData = () => { setLoading(true); setError(null); setData(null); setTimeout(() => { const success = Math.random() > 0.5; if (success) { setData('Here is your data!'); setLoading(false); } else { setError('Failed to load data.'); setLoading(false); } }, 2000); }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> {loading && <ActivityIndicator size="large" color="#0000ff" />} {error && <Text style={{ color: 'red', marginBottom: 10 }}>{error}</Text>} {data && <Text style={{ marginBottom: 10 }}>{data}</Text>} {!loading && !data && !error && <Text>Press the button to load data.</Text>} <Button title="Load Data" onPress={fetchData} /> </View> ); }
Important Notes
Always give users feedback when waiting or when errors happen.
Use clear and simple messages for errors.
Spinners or progress bars help users understand the app is working.
Summary
Loading states show when the app is busy getting data.
Error states show when something goes wrong.
Use states in React Native to control what the user sees.
Practice
1. What is the main purpose of a loading state in a React Native app?
easy
Solution
Step 1: Understand loading state role
Loading states inform users that the app is busy fetching or processing data.Step 2: Differentiate from error state
Error states show problems, not loading progress.Final Answer:
To show the user that data is being fetched or processed -> Option BQuick Check:
Loading state = show progress [OK]
Hint: Loading means waiting for data, not errors [OK]
Common Mistakes:
- Confusing loading with error states
- Thinking loading hides content permanently
- Assuming loading restarts the app
2. Which of the following is the correct way to declare a loading state using React Native's useState hook?
easy
Solution
Step 1: Recall useState syntax
useState returns an array with current value and setter function, usually destructured.Step 2: Check options for correct destructuring
const [loading, setLoading] = useState(false); correctly uses array destructuring with const and initial value false.Final Answer:
const [loading, setLoading] = useState(false); -> Option DQuick Check:
useState destructuring = const [loading, setLoading] = useState(false); [OK]
Hint: useState returns [value, setter], destructure with const [a,b] [OK]
Common Mistakes:
- Not destructuring the array from useState
- Using let or var instead of const
- Assigning useState directly to a variable without destructuring
3. Given this React Native snippet, what will be displayed when
loading is true?const [loading, setLoading] = useState(true); return ( loading ? Loading... : Data loaded );
medium
Solution
Step 1: Check the value of loading
loading is true initially.Step 2: Evaluate the ternary expression
If loading is true, Loading... is returned and shown.Final Answer:
Loading... -> Option AQuick Check:
loading true shows loading text [OK]
Hint: Ternary shows first option if condition true [OK]
Common Mistakes:
- Confusing true and false branches
- Thinking nothing shows when loading is true
- Assuming error message shows automatically
4. Identify the error in this React Native code handling loading and error states:
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
if (loading) {
return Loading...;
} else if (error) {
return Error occurred;
}
return Data loaded;medium
Solution
Step 1: Check initial loading state
Loading is false initially, so loading screen never shows on start.Step 2: Understand typical loading flow
Loading should start as true while fetching data, then false after.Final Answer:
Loading state should be true initially to show loading -> Option CQuick Check:
Initial loading true needed to show loading [OK]
Hint: Loading usually starts true before data fetch [OK]
Common Mistakes:
- Setting loading false initially hides loading screen
- Thinking error must be boolean
- Assuming if-else order is wrong
5. You want to show a loading spinner while fetching data, then show an error message if fetch fails, or the data if successful. Which React Native pattern correctly handles these states?
hard
Solution
Step 1: Understand state separation
Separate states for loading, error, and data allow clear control of UI for each condition.Step 2: Evaluate options
Use separate states: loading (boolean), error (string|null), data (object|null), and conditionally render based on these uses best practice: boolean loading, error message or null, and data object or null.Final Answer:
Use separate states: loading (boolean), error (string|null), data (object|null), and conditionally render based on these -> Option AQuick Check:
Separate states for clarity and control [OK]
Hint: Use separate states for loading, error, and data [OK]
Common Mistakes:
- Ignoring error handling
- Using one string state for all conditions
- Rendering data without waiting for fetch
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
