I am totally confused about what's happening here. I am setting the state in React but it's updating very late. Here is the function:
fetchTimesheets() {
const userId = cryptInfo.decrypt(localStorage.getItem('user_id'))
var userArray = []
var timeSheets = []
fetchManagerProject(userId)
.then(async (res) => {
const projects = await res.json()
projects.forEach(project => {
project.teammember && project.teammember.forEach(member => {
if (userArray.indexOf(member.user) === -1) {
userArray.push(member.user)
fetchUserTimelogs(member.user)
.then(async (res) => {
const timesheet = await res.json()
if (timesheet)
timesheet.forEach(sheet => {
timeSheets.push(sheet)
});
})
}
})
})
this.setState({
timesheets: timeSheets
})
})
}
I am calling this function on componentDidMount
method
componentDidMount() {
this.fetchTimesheets()
}
But I am getting that my value is evaluated just now and state is not updated. I have seen many questions related to this but didn't get a good solution.