Fetching data asynchronously in useEffect

Posted on

To asynchronously fetch data when a component mounts, define and then call an async function inside of a useEffect with an empty dependency array.

useEffect(() => {
  const fetchData = async () => {
    const data = await getData()
    setData(data)
  }
 
  fetchData()
}, [])
useEffect(() => {
  const fetchData = async () => {
    const data = await getData()
    setData(data)
  }
 
  fetchData()
}, [])
Debug
none
Grid overlay