This React Native app uses Axios to get a todo item from the internet. It shows a loading spinner while waiting. If it fails, it shows an error message. If it works, it shows the todo details.
import React, {useEffect, useState} from 'react';
import {View, Text, ActivityIndicator} from 'react-native';
import axios from 'axios';
export default function App() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
axios.get('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
setData(response.data);
setLoading(false);
})
.catch(err => {
setError('Failed to load data');
setLoading(false);
});
}, []);
if (loading) return <ActivityIndicator size="large" />;
if (error) return <Text>{error}</Text>;
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20}}>
<Text style={{fontSize: 18, fontWeight: 'bold'}}>Todo Item:</Text>
<Text>ID: {data.id}</Text>
<Text>Title: {data.title}</Text>
<Text>Completed: {data.completed ? 'Yes' : 'No'}</Text>
</View>
);
}