0

there. I have a question about using Axios for connecting apis. Here is the snippet of my code:

const [resData, setResData] = useState();

for(let i = 0; i < 5; i++) {
  axios.put('api', params, config).then(res => setResData(prev => ({...prev, res.data.result})))
}

useEffect(() => {
  if(resData.length === 5) {
    axios.post('nextApi', {data: resData}, config)
  }
}, [resData])

The logic is that I receive data from calling an api and I gather them and make a group and then I call the nextApi by sending the group of data I received from the previous api.

The problem is that I need to loop axios couple of times, so I tried to call the nextApi when receiving response, but failed. I currently only think of this way, but it looks kinda of complicated. I do not think I can understand my code in the future.

Is there a better way to complete the process?

  • First of all, why call the same API five times? A batch API is better. – Lin Du May 22 '23 at 12:59
  • Does this answer your question? [React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing](https://stackoverflow.com/questions/53332321/react-hook-warnings-for-async-function-in-useeffect-useeffect-function-must-ret) – Aluan Haddad May 22 '23 at 14:07

0 Answers0