Consider this React Native component using Axios to fetch user data on button press. What will be displayed after pressing the button?
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> ); }
Think about what the Axios call returns and how state updates the displayed text.
The component starts with user as null, so it shows 'No user loaded'. When the button is pressed, Axios fetches user data and sets user to the response. The user name 'Leanne Graham' is then displayed.
Given this component, when is the Axios GET request triggered?
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>; }
Look at the dependency array in useEffect.
The empty dependency array [] means the effect runs only once after the component mounts, triggering the Axios GET request once.
Choose the correct Axios POST request syntax to send JSON data { name: 'John' } to the endpoint.
Axios automatically sets the correct headers and stringifies the data object.
Option A correctly passes the data object directly. Axios sets the Content-Type to application/json and stringifies the object automatically. Option A sends a string but Axios expects an object. Option A sets wrong headers. Option A misuses the config object.
Examine this code snippet. What causes the runtime error?
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();
Look at how fetchData is called.
The function fetchData is async but called without awaiting or handling its promise. This can cause unhandled promise rejections or unexpected behavior. The code itself is valid but best practice is to await or handle the promise.
Which statement best describes the purpose of Axios interceptors in a React Native app?
Think about how interceptors can change requests or responses.
Axios interceptors let you add code to run before a request is sent or before a response is handled. This is useful for adding headers, logging, or error handling globally. They do not automatically retry, cache, or convert calls.