I have a state variable that I need to update after all the API calls in the loop are finished. As of now the state is updating before the API calls have completed. SetResults is updating too quickly.
const [results, setResults] = useState([]);
const getResults = () => {
let copy = [...results];
for (let item of arr) {
API.getItemById(item)
.then(res => {
copy.push(res.data);
})
setResults(copy);
}
Could someone help me out here?