What if your app could talk to users when it's busy or stuck?
Why Loading and error states in React Native? - Purpose & Use Cases
Imagine you open a mobile app to see your messages, but nothing happens for a long time. You wonder if the app is broken or if your internet is slow. You try tapping buttons repeatedly, unsure if the app is working.
Without clear loading or error messages, users get confused and frustrated. They might think the app crashed or ignore it altogether. Developers struggle to show what is happening behind the scenes, making the app feel unreliable.
Loading and error states give users clear signs when the app is busy or something went wrong. A spinner or message tells them to wait or try again. This simple feedback makes the app feel smart and trustworthy.
fetchData(); // no feedback to user
setLoading(true);
fetchData()
.then(() => setLoading(false))
.catch(() => {
setLoading(false);
setError(true);
});Users stay informed and confident, improving their experience and trust in your app.
When you order food in a delivery app, a loading spinner shows your order is processing. If the internet drops, an error message helps you retry instead of guessing what happened.
Loading and error states communicate app status clearly.
They prevent user confusion and frustration.
Implementing them makes apps feel reliable and user-friendly.
Practice
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]
- Confusing loading with error states
- Thinking loading hides content permanently
- Assuming loading restarts the app
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]
- Not destructuring the array from useState
- Using let or var instead of const
- Assigning useState directly to a variable without destructuring
loading is true?const [loading, setLoading] = useState(true); return ( loading ? Loading... : Data loaded );
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]
- Confusing true and false branches
- Thinking nothing shows when loading is true
- Assuming error message shows automatically
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
if (loading) {
return Loading...;
} else if (error) {
return Error occurred;
}
return Data loaded;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]
- Setting loading false initially hides loading screen
- Thinking error must be boolean
- Assuming if-else order is wrong
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]
- Ignoring error handling
- Using one string state for all conditions
- Rendering data without waiting for fetch
Ready to prove it?
Pass the test and this pattern moves unknown
