Bird
Raised Fist0
React Nativemobile~20 mins

Loading and error states in React Native - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Loading and Error States Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What will this React Native component display during data loading?

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?

React Native
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>;
}
AA large spinning loader is shown for 3 seconds, then 'Hello!' text appears.
BThe text 'Hello!' appears immediately, then disappears after 3 seconds.
CNothing is shown until the data loads, then 'Hello!' appears.
DAn error message is shown because the data is null initially.
Attempts:
2 left
💡 Hint

Look at the loading state and what the component returns when loading is true.

❓ ui_behavior
intermediate
2:00remaining
Which option correctly shows an error message when data fetching fails?

This React Native component tries to fetch data but may fail. Which option correctly displays an error message if the fetch fails?

React Native
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>;
}
AShows spinner forever because error is ignored.
BShows 'Error: Network error' immediately, then spinner after 2 seconds.
CShows blank screen because data is null.
DShows a spinner for 2 seconds, then displays 'Error: Network error' text.
Attempts:
2 left
💡 Hint

Check the order of the if statements and when loading and error change.

❓ lifecycle
advanced
2:00remaining
What happens if the component unmounts before data loads?

In this React Native component, what issue can occur if the component unmounts before the data finishes loading?

React Native
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>;
}
AA memory leak occurs because the timer is not cleared.
BNo issue because the timer is cleared on unmount.
CThe app crashes because setData is called after unmount.
DThe component shows 'Loading...' forever.
Attempts:
2 left
💡 Hint

Look at the cleanup function returned by useEffect.

📝 Syntax
advanced
2:00remaining
What error does this code produce when loading state is missing?

What error will this React Native component produce when it tries to render?

React Native
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>;
}
ANo error, shows 'Loading...' then 'Done'
BTypeError: Cannot read property 'loading' of undefined
CReferenceError: loading is not defined
DSyntaxError: Unexpected identifier
Attempts:
2 left
💡 Hint

Check if loading is declared anywhere in the component.

🧠 Conceptual
expert
2:00remaining
Why is it important to show both loading and error states in mobile apps?

Choose the best explanation for why mobile apps should show clear loading and error states to users.

AIt improves user experience by informing users about app status and prevents confusion.
BIt is only needed for debugging and has no impact on user experience.
CIt slows down the app but looks nicer visually.
DIt is required by app stores to pass submission.
Attempts:
2 left
💡 Hint

Think about how users feel when waiting or when something goes wrong.

Practice

(1/5)
1. What is the main purpose of a loading state in a React Native app?
easy
A. To display an error message when something goes wrong
B. To show the user that data is being fetched or processed
C. To permanently hide content from the user
D. To restart the app automatically

Solution

  1. Step 1: Understand loading state role

    Loading states inform users that the app is busy fetching or processing data.
  2. Step 2: Differentiate from error state

    Error states show problems, not loading progress.
  3. Final Answer:

    To show the user that data is being fetched or processed -> Option B
  4. Quick 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
A. const loading = useState(false);
B. let loading = useState(true);
C. var loading = useState(false);
D. const [loading, setLoading] = useState(false);

Solution

  1. Step 1: Recall useState syntax

    useState returns an array with current value and setter function, usually destructured.
  2. Step 2: Check options for correct destructuring

    const [loading, setLoading] = useState(false); correctly uses array destructuring with const and initial value false.
  3. Final Answer:

    const [loading, setLoading] = useState(false); -> Option D
  4. Quick 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
A. Loading...
B. Nothing is displayed
C. Data loaded
D. Error message

Solution

  1. Step 1: Check the value of loading

    loading is true initially.
  2. Step 2: Evaluate the ternary expression

    If loading is true, Loading... is returned and shown.
  3. Final Answer:

    Loading... -> Option A
  4. Quick 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
A. Error state should be a boolean, not null
B. The if-else logic is reversed
C. Loading state should be true initially to show loading
D. No error, code works fine

Solution

  1. Step 1: Check initial loading state

    Loading is false initially, so loading screen never shows on start.
  2. Step 2: Understand typical loading flow

    Loading should start as true while fetching data, then false after.
  3. Final Answer:

    Loading state should be true initially to show loading -> Option C
  4. Quick 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
A. Use separate states: loading (boolean), error (string|null), data (object|null), and conditionally render based on these
B. Only use a loading state and ignore errors
C. Use a single state variable for all three states with strings 'loading', 'error', 'data'
D. Render data immediately without any loading or error checks

Solution

  1. Step 1: Understand state separation

    Separate states for loading, error, and data allow clear control of UI for each condition.
  2. 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.
  3. Final Answer:

    Use separate states: loading (boolean), error (string|null), data (object|null), and conditionally render based on these -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard