我做了我的componentWillMount()异步。现在,我可以用await用setState。
这是示例代码:
componentWillMount = async() => {
const { fetchRooms } = this.props
await this.setState({ })
fetchRooms()
}
所以这里的问题是this.setState返回Promise,因为我可以使用await它?
编辑
当我放置 await 然后它按顺序运行1, 2, 3当我删除 await 然后它运行1, 3, 2??
componentWillMount = async() => {
const { fetchRooms } = this.props
console.log(1)
await this.setState({ } => {
console.log(2)
})
console.log(3)
fetchRooms()
}