Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

useeffect async not working

function Example() {
  const [data, dataSet] = useState<any>(null)

  useEffect(() => {
    async function fetchMyAPI() { // make a new async function with a function name
      let response = await fetch('api/data')
      response = await response.json()
      dataSet(response)
    }

    fetchMyAPI() // call the unction inside the useEffect()
  }, [])

  return <div>{JSON.stringify(data)}</div>
}
 
PREVIOUS NEXT
Tagged: #useeffect #async #working
ADD COMMENT
Topic
Name
8+1 =