This React Native app fetches a todo item from a backend API and shows its details. It shows a loading spinner while waiting and an error message if something goes wrong.
import React, { useEffect, useState } from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
export default function App() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => {
setData(json);
setLoading(false);
})
.catch(err => {
setError(err.message);
setLoading(false);
});
}, []);
if (loading) return <ActivityIndicator size="large" />;
if (error) return <Text>Error: {error}</Text>;
return (
<View style={{ padding: 20 }}>
<Text>Todo Item:</Text>
<Text>ID: {data.id}</Text>
<Text>Title: {data.title}</Text>
<Text>Completed: {data.completed ? 'Yes' : 'No'}</Text>
</View>
);
}