Answer the question
In order to leave comments, you need to log in
How to pass array from Promise to useState?
I got acquainted with promise and as I understand it, there is an alternative to async \ await and I don’t understand a little how to pull the value out of the promise if I use, for example, api (axios) for a get request.
Based on my experience - I get an array of data inside .then and can I use it only inside it? or can i pass a value to useState?
I'm a little confused with the output of the value, help
const [dataItem, setDataItem] = useState([])
const getDataItem = new Promise((resolve) => {
setTimeout(() => {
axios.get('https://1111111111111111111111111.mockapi.io/api/v1/food/')
.then(function(response) {
resolve(response)
})
}, 300)
})
getDataItem.then((data)=> setDataItem(data))
console.log(dataItem)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question