Bird
Raised Fist0
React Nativemobile~10 mins

Fetch API for GET requests in React Native - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fetch data from the URL using fetch.

React Native
fetch('[1]')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));
Drag options to blanks, or click blank then click option'
A"https://api.example.com/data"
Bresponse.json
Cconsole.log
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the URL in quotes.
Passing a function instead of a URL.
2fill in blank
medium

Complete the code to convert the fetch response to JSON.

React Native
fetch('https://api.example.com/data')
  .then(response => response.[1]())
  .then(data => console.log(data));
Drag options to blanks, or click blank then click option'
AformData
Btext
Cblob
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using response.text() instead of response.json().
Forgetting to call the method with parentheses.
3fill in blank
hard

Fix the error in the fetch call to handle errors properly.

React Native
fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .[1](error => console.error('Error:', error));
Drag options to blanks, or click blank then click option'
Afinally
Bthen
Ccatch
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using then instead of catch for error handling.
Not handling errors at all.
4fill in blank
hard

Fill both blanks to create a React Native component that fetches data on button press.

React Native
import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';

export default function FetchExample() {
  const [data, setData] = useState(null);

  const fetchData = () => {
    fetch('https://api.example.com/data')
      .then(response => response.[1]())
      .then(json => setData(json))
      .catch(error => console.error(error));
  };

  return (
    <View>
      <Button title="Fetch Data" onPress=[2] />
      {data && <Text>{JSON.stringify(data)}</Text>}
    </View>
  );
}
Drag options to blanks, or click blank then click option'
Ajson
BfetchData()
CfetchData
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Calling fetchData() inside onPress instead of passing the function.
Using response.text() instead of response.json().
5fill in blank
hard

Fill all three blanks to fetch data and display loading status in React Native.

React Native
import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';

export default function FetchWithLoading() {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(false);

  const fetchData = () => {
    setLoading([1]);
    fetch('https://api.example.com/data')
      .then(response => response.[2]())
      .then(json => {
        setData(json);
        setLoading([3]);
      })
      .catch(error => {
        console.error(error);
        setLoading(false);
      });
  };

  return (
    <View>
      <Button title="Fetch Data" onPress={fetchData} />
      {loading ? <Text>Loading...</Text> : data && <Text>{JSON.stringify(data)}</Text>}
    </View>
  );
}
Drag options to blanks, or click blank then click option'
Atrue
Bjson
Cfalse
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting loading to true before fetch.
Using response.text() instead of response.json().
Not setting loading to false after fetch.

Practice

(1/5)
1.

What does the Fetch API do in React Native?

Choose the best description.

easy
A. It retrieves data from a web server using HTTP requests.
B. It styles components with CSS.
C. It manages app navigation between screens.
D. It stores data locally on the device.

Solution

  1. Step 1: Understand Fetch API purpose

    The Fetch API is designed to make network requests to get or send data.
  2. Step 2: Match description to Fetch API

    Only It retrieves data from a web server using HTTP requests. describes retrieving data from a web server, which is what Fetch does.
  3. Final Answer:

    It retrieves data from a web server using HTTP requests. -> Option A
  4. Quick Check:

    Fetch API = Data retrieval [OK]
Hint: Fetch is for network data requests, not styling or storage [OK]
Common Mistakes:
  • Confusing Fetch with styling or navigation libraries
  • Thinking Fetch stores data locally
  • Assuming Fetch only works with JSON
2.

Which of the following is the correct syntax to fetch JSON data from https://api.example.com/data?

fetch(_____).then(response => response.json())
easy
A. "https://api.example.com/data"
B. url
C. https://api.example.com/data
D. 'https://api.example.com/data'

Solution

  1. Step 1: Identify correct string syntax in JavaScript

    URLs must be passed as strings, enclosed in quotes.
  2. Step 2: Choose proper quotes for URL string

    'https://api.example.com/data' uses single quotes correctly.
  3. Final Answer:

    'https://api.example.com/data' -> Option D
  4. Quick Check:

    URL as string = 'https://api.example.com/data' [OK]
Hint: Always wrap URLs in quotes for fetch [OK]
Common Mistakes:
  • Not using quotes around the URL
  • Using undefined variable url without declaration
  • Using double quotes inside double quotes without escaping
3.

What will be logged to the console after running this code?

fetch('https://api.example.com/user')
  .then(response => response.json())
  .then(data => console.log(data.name))
  .catch(error => console.error(error));

Assume the server returns { "name": "Alice" }.

medium
A. SyntaxError
B. undefined
C. Alice
D. null

Solution

  1. Step 1: Understand fetch and JSON parsing

    The fetch call gets the response, then response.json() parses it into an object.
  2. Step 2: Access the 'name' property from parsed data

    The data object has a 'name' key with value "Alice", so console.log(data.name) prints "Alice".
  3. Final Answer:

    Alice -> Option C
  4. Quick Check:

    Parsed JSON name = Alice [OK]
Hint: Parsed JSON data properties print their values [OK]
Common Mistakes:
  • Logging before JSON parsing
  • Trying to access data.name before fetch resolves
  • Ignoring asynchronous nature of fetch
4.

Identify the error in this fetch code snippet:

fetch('https://api.example.com/items')
  .then(response => response.json)
  .then(data => console.log(data))
  .catch(error => console.error(error));
medium
A. Missing parentheses after response.json() call
B. URL string is not quoted properly
C. No catch block to handle errors
D. Using console.log instead of alert

Solution

  1. Step 1: Check response.json usage

    response.json is a function and must be called with parentheses to parse JSON.
  2. Step 2: Identify missing parentheses error

    The code uses response.json without () which returns the function itself, not the parsed data.
  3. Final Answer:

    Missing parentheses after response.json() call -> Option A
  4. Quick Check:

    Call response.json() with () [OK]
Hint: Always call response.json() with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on response.json
  • Misplacing catch block
  • Using wrong quotes for URL
5.

You want to fetch user data and show a loading message while waiting. Which code snippet correctly handles loading state and errors?

const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState(null);

React.useEffect(() => {
  fetch('https://api.example.com/user')
    .then(res => res.json())
    .then(data => {
      // handle data
      setLoading(false);
    })
    .catch(err => {
      setError(err.message);
      setLoading(false);
    });
}, []);

What is missing to properly show loading and error states?

hard
A. Remove catch block to avoid errors
B. Render UI that shows loading and error messages based on state
C. Call setLoading(true) inside fetch then block
D. Use async/await instead of then/catch

Solution

  1. Step 1: Understand state variables usage

    loading and error states are set correctly during fetch lifecycle.
  2. Step 2: Identify missing UI rendering logic

    Without UI code that checks loading and error states, user won't see messages.
  3. Final Answer:

    Render UI that shows loading and error messages based on state -> Option B
  4. Quick Check:

    Show loading/error UI based on state [OK]
Hint: Set state is not enough; render UI accordingly [OK]
Common Mistakes:
  • Not rendering loading or error messages
  • Setting loading true too late
  • Ignoring error handling in UI
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