我做了我的componentWillMount()
异步。现在我可以使用await
与setState
.
这是示例代码:
componentWillMount = async() => {
const { fetchRooms } = this.props
await this.setState({ })
fetchRooms()
}
所以这里的问题是this.setState
返回承诺,因为我可以使用await
用它?
Edit
当我放置等待时,它会按顺序运行1, 2, 3
当我删除等待时,它就会运行1, 3, 2
??
componentWillMount = async() => {
const { fetchRooms } = this.props
console.log(1)
await this.setState({ } => {
console.log(2)
})
console.log(3)
fetchRooms()
}
你可以承诺this.setState
这样你就可以使用 React API 作为承诺。这就是我让它工作的方式:
class LyricsGrid extends Component {
setAsyncState = (newState) =>
new Promise((resolve) => this.setState(newState, resolve));
后来我打电话this.setAsyncState
使用标准 Promise API:
this.setAsyncState({ lyricsCorpus, matrix, count })
.then(foo1)
.then(foo2)
.catch(err => console.error(err))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)