Thursday, November 1, 2018

javascript - Will there be an issue when this.setState is used inside of .then?

While I was working on a project, there some case that I have to change a state when user update a data.



Here is the code :



     state = {
changedReservationStatus: toJS(this.props.reservationViewModel.getReservation())
}

reloadPageData = async () => {


const { reservationViewModel, reservationOrderViewModel } = this.props

const reservation = toJS(reservationViewModel.getReservation())

await reservationViewModel.fetchReservation(reservation.id).then(changedReservationStatus => this.setState({ changedReservationStatus }))

}


( reservation variable contains a data of an array. )




When user clicks save button, reloadPageData function works and then await reservationViewModel.fetchReservation starts to work, it triggers this.setState after .then statement.



But as you may know reservationViewModel.fetchReservation and this.setState both work asynchronously.



when I console.log this.state.changedReservationStatus inside render function, it renders the data I want to get.



But is it okay to use this.setState inside of Promise?



Will there be any issue?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...