0
0
React Nativemobile~20 mins

Axios library in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axios Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output when fetching data with Axios in React Native?

Consider this React Native component using Axios to fetch user data on button press. What will be displayed after pressing the button?

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

export default function UserFetcher() {
  const [user, setUser] = useState(null);
  const fetchUser = () => {
    axios.get('https://jsonplaceholder.typicode.com/users/1')
      .then(response => setUser(response.data))
      .catch(() => setUser({ name: 'Error' }));
  };
  return (
    <View>
      <Button title="Fetch User" onPress={fetchUser} />
      <Text>{user ? user.name : 'No user loaded'}</Text>
    </View>
  );
}
AInitially blank, then shows 'Error' after button press
BShows 'No user loaded' always, button press does nothing
CInitially shows 'No user loaded', then after button press shows 'Leanne Graham'
DShows 'No user loaded' initially, then blank after button press
Attempts:
2 left
💡 Hint

Think about what the Axios call returns and how state updates the displayed text.

lifecycle
intermediate
2:00remaining
When does Axios fetch data in this React Native component?

Given this component, when is the Axios GET request triggered?

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

export default function DataLoader() {
  const [data, setData] = useState(null);
  useEffect(() => {
    axios.get('https://jsonplaceholder.typicode.com/posts/1')
      .then(response => setData(response.data))
      .catch(() => setData({ title: 'Error' }));
  }, []);
  return <View><Text>{data ? data.title : 'Loading...'}</Text></View>;
}
AWhen the component unmounts
BOnly once when the component first mounts
CEvery time the component re-renders
DWhen the user presses a button
Attempts:
2 left
💡 Hint

Look at the dependency array in useEffect.

📝 Syntax
advanced
2:00remaining
Which Axios call correctly sends JSON data in a POST request?

Choose the correct Axios POST request syntax to send JSON data { name: 'John' } to the endpoint.

Aaxios.post('https://api.example.com/users', { name: 'John' })
Baxios.post('https://api.example.com/users', JSON.stringify({ name: 'John' }))
Caxios.post('https://api.example.com/users', { name: 'John' }, { headers: { 'Content-Type': 'text/plain' } })
Daxios.post('https://api.example.com/users', null, { data: { name: 'John' } })
Attempts:
2 left
💡 Hint

Axios automatically sets the correct headers and stringifies the data object.

🔧 Debug
advanced
2:00remaining
Why does this Axios request cause a runtime error?

Examine this code snippet. What causes the runtime error?

React Native
import axios from 'axios';

async function fetchData() {
  try {
    const response = await axios.get('https://api.example.com/data');
    console.log(response.data);
  } catch (error) {
    console.log(error.message);
  }
}

fetchData();
AThe function fetchData is not awaited or handled properly
BMissing import of React in the file
Caxios.get URL is missing protocol
DUsing await outside an async function
Attempts:
2 left
💡 Hint

Look at how fetchData is called.

🧠 Conceptual
expert
2:00remaining
What is the effect of setting Axios interceptors in React Native?

Which statement best describes the purpose of Axios interceptors in a React Native app?

AThey convert all Axios calls to fetch API calls internally
BThey automatically retry failed requests without any code
CThey cache all responses locally to improve performance
DThey allow modifying requests or responses globally before they are handled by then or catch
Attempts:
2 left
💡 Hint

Think about how interceptors can change requests or responses.