0

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?

Robert
  • 11
  • 1
  • 5
  • Take a look at my github example here. I used RxJS to handle a series of requests. https://github.com/loursbourg/rxjs-request-chain – Kadir Damene Sep 10 '21 at 20:57

0 Answers0