The*_*rus 5 javascript asynchronous setstate reactjs
这是我的componentDidMount方法.我想设置当前用户的状态,然后在设置该用户时调用该函数.我怎样才能做到这一点?
componentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user})
}
});
this.props.retrieveMatches(this.state.user.uid)
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用async/await但是我没有在这里正确使用它:
async componentDidMount = () => {
await firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user})
}
});
this.props.retrieveMatches(this.state.user.uid)
}
Run Code Online (Sandbox Code Playgroud)
基本上我想在第7行调用道具功能之前等待第2-6行
你需要使用 的.setState()回调函数:
componentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user}, () => {
this.props.retrieveMatches(this.state.user.uid);
})
}
});
}
Run Code Online (Sandbox Code Playgroud)
问候
| 归档时间: |
|
| 查看次数: |
1508 次 |
| 最近记录: |