I am working with ReactJS, and ran into a problem. I used setState multiple times after getting my response.data from axios in order to set all the neccessary data into my state. However, the issue is that I want to wait for all the setStates to finish before moving on to the next line of code. I am not sure how to do that, as I know that the setState takes in a second callback function, but thats only for a single setState. I tried looking into Promise but I am not sure how to implement that correctly.
Here is my code:
axiosInstance.get(`/customer-converted/?group=department&metric=${metric}&start=${start}&end=${end}`)
.then(response => {
this.setState({ data: response.data });
let rangeStart = (response.data.length > 3) ? response.data[response.data.length - 3].date : response.data[0].date
let rangeEnd = response.data[response.data.length - 1].date
this.setState({ start: rangeStart })
this.setState({ end: rangeEnd })
// execute next line of code here only after all setState above are finished
})
All help is appreciated, thanks all!