react fetch state data in useEffect code example
Example 1: how to fetch data with useEffect
useEffect(() => {
const fetchData = async () => {
const result = await fetch(
'https://my-api.com',
);
setState(result);
};
fetchData();
}, []);
Example 2: How to fetch data from an api useEffect
import {useEffect} from "react";
useEffect(() => {
fetchData();
}, []);