I’m getting this error back though this same format seems to work in examples.
const response = await axios.get('http://localhost:5000/get-tasks')
const dataObject = response.data
const arrayOfKeys = Object.keys(dataObject)
const arrayOfData = Object.keys(dataObject).map((key) => dataObject[key])
console.log(arrayOfKeys)
console.log(arrayOfData)
}, [])```
2
Answers
async
functions return aPromise
, anduseEffect
does not expect aPromise
returned. (If anything is returned at all, it should be a function for cleanup of any effects when a component is unmounted.)If you want to use async/await in
useEffect
then you can wrap the whole thing in anasync
IIFE. For example:Alternatively, you can use
.then()
on the innerasync
operation instead ofawait
:The useEffect function arg can’t be asynchronous, but you can call an async function inside it.