← Каталог
React — компоненты-рецепты — Список заметок с API
Фрагмент из «React — компоненты-рецепты»: Список заметок с API.
import { useEffect, useState } from 'react';
export default function NotesList() {
const [notes, setNotes] = useState([]);
const [error, setError] = useState('');
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch('http://127.0.0.1:3000/notes')
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
})
.then(setNotes)
.catch((e) => setError(e.message))
.finally(() => setLoading(false));
}, []);
if (loading) return <p>Загрузка…</p>;
if (error) return <p className="error">Ошибка: {error}</p>;
if (!notes.length) return <p>Заметок пока нет</p>;
return (
<ul>
{notes.map((n) => (
<li key={n.id}>{n.text}</li>
))}
</ul>
);
} import { useEffect, useState } from 'react';
export default function NotesList() {
const [notes, setNotes] = useState([]);
const [error, setError] = useState('');
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch('http://127.0.0.1:3000/notes')
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
})
.then(setNotes)
.catch((e) => setError(e.message))
.finally(() => setLoading(false));
}, []);
if (loading) return <p>Загрузка…</p>;
if (error) return <p className="error">Ошибка: {error}</p>;
if (!notes.length) return <p>Заметок пока нет</p>;
return (
<ul>
{notes.map((n) => (
<li key={n.id}>{n.text}</li>
))}
</ul>
);
}