0
0
React Nativemobile~20 mins

Fetch API for GET requests in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fetch API 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 after fetching data?
Consider this React Native component that fetches user data from an API and displays the user's name. What will be shown on the screen after the fetch completes successfully?
React Native
import React, { useState, useEffect } from 'react';
import { Text, View } from 'react-native';

export default function UserName() {
  const [name, setName] = useState('Loading...');

  useEffect(() => {
    fetch('https://api.example.com/user/1')
      .then(response => response.json())
      .then(data => setName(data.name))
      .catch(() => setName('Error'));
  }, []);

  return (
    <View>
      <Text>{name}</Text>
    </View>
  );
}
ADisplays 'Loading...' forever because useEffect is missing dependencies.
BDisplays the user's name fetched from the API, e.g., 'Alice'.
CDisplays 'Error' because fetch is not awaited.
DDisplays nothing because the component returns null.
Attempts:
2 left
💡 Hint
Think about what happens after the fetch promise resolves and how state updates affect rendering.
📝 Syntax
intermediate
1:30remaining
Which fetch call syntax correctly performs a GET request?
You want to fetch data from 'https://api.example.com/items' using fetch in React Native. Which option correctly performs a GET request?
Afetch('https://api.example.com/items', { body: '{}' })
Bfetch('https://api.example.com/items', { method: 'POST' })
Cfetch('https://api.example.com/items', { method: 'GET' })
Dfetch('https://api.example.com/items', { headers: { 'Content-Type': 'application/json' } })
Attempts:
2 left
💡 Hint
GET is the default method for fetch, but specifying it explicitly is correct.
lifecycle
advanced
1:30remaining
When does the fetch call inside useEffect run in this React Native component?
Given this component, when will the fetch call execute?
React Native
import React, { useEffect } from 'react';
import { Text } from 'react-native';

export default function DataFetcher() {
  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(res => res.json())
      .then(console.log);
  }, []);

  return <Text>Fetching data...</Text>;
}
AOnly once, right after the component mounts.
BEvery time the component re-renders.
COnly when the component unmounts.
DEvery time the user taps the screen.
Attempts:
2 left
💡 Hint
Look at the dependency array in useEffect.
🔧 Debug
advanced
2:00remaining
What error does this fetch code produce?
This React Native code tries to fetch JSON data but has a bug. What error will it cause?
React Native
fetch('https://api.example.com/data')
  .then(response => response.json)
  .then(data => console.log(data));
AReferenceError: data is not defined
BSyntaxError: Unexpected token in JSON
CTypeError: response.json is not a function
DNo error, logs the response.json function reference instead of the data
Attempts:
2 left
💡 Hint
Check how response.json is called.
🧠 Conceptual
expert
2:00remaining
Why should you handle errors when using fetch for GET requests in React Native?
Which is the best reason to handle errors when fetching data with fetch in React Native?
ABecause network requests can fail due to no internet or server issues, and the app should respond gracefully.
BBecause fetch automatically retries failed requests unless errors are handled.
CBecause fetch throws a syntax error if the URL is a string.
DBecause handling errors makes the fetch call faster.
Attempts:
2 left
💡 Hint
Think about real-life situations like losing internet connection.