Skip to main content

Run Effect with async await Fetch API

const [value, setValue] = useState(null)
useEffect(() => {
async function fetchData() {
const res = await fetch(url)
const data = await res.json()
}
fetchData()
}, [])
// DON'T DOT THIS
useEffect(async () => {
const res = await fetch("_SOME_URL")
const data = await res.json()
setValue(data)
}, [])

Effect callbacks are synchronous to prevent race conditions. Put the asyn function inside.

References