Код IT
← Каталог

React — Router, данные с API и оптимизация — Данные с сервера

Фрагмент из «React — Router, данные с API и оптимизация»: Данные с сервера.

javascript javascriptencyclopedia3-ecosystem-2-frontend-frameworks-1-react-277 embed URL статья в энциклопедии
Plain text main.jsx
const [notes, setNotes] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

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(setError)
    .finally(() => setLoading(false));
}, []);
const [notes, setNotes] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

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(setError)
    .finally(() => setLoading(false));
}, []);