0
0
React Nativemobile~20 mins

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

Choose your learning style9 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.