admin管理员组文章数量:1332403
This is my ponentDidMount
method. I want to set the state of the current user and then call the function when that user is set. How can I do this?
ponentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user})
}
});
this.props.retrieveMatches(this.state.user.uid)
}
I've tried using async/await but im not using it correctly here:
async ponentDidMount = () => {
await firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user})
}
});
this.props.retrieveMatches(this.state.user.uid)
}
basically I want to await for lines 2-6 before calling the props function on line 7
This is my ponentDidMount
method. I want to set the state of the current user and then call the function when that user is set. How can I do this?
ponentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user})
}
});
this.props.retrieveMatches(this.state.user.uid)
}
I've tried using async/await but im not using it correctly here:
async ponentDidMount = () => {
await firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user})
}
});
this.props.retrieveMatches(this.state.user.uid)
}
basically I want to await for lines 2-6 before calling the props function on line 7
Share Improve this question asked Jan 29, 2018 at 14:29 The WalrusThe Walrus 1,2087 gold badges31 silver badges49 bronze badges4 Answers
Reset to default 4you need to use .setState()
's callback function:
ponentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user}, () => {
this.props.retrieveMatches(this.state.user.uid);
})
}
});
}
greetings
I understand the confusion but that line uses callbacks and not Promises so you're not supposed to use async/await
it should be:
ponentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user}, () => { this.props.retrieveMatches(this.state.user.uid); })
}
});
}
You can use async/await to replace promises then and catch calls
This
promise.then((result) => {...}).catch((error) => {});
would bee
try {
const result = await promise();
} catch (error) {
// do stuff
}
you shouldn't make a react lifecycle method async.
do an async await method externally as a helper function then import it:
in a helper file:
async asynchronousFn() {
const result = await (your asynchronous code)
return result
}
in the ponent:
ponentDidMount() {
asynchronousfn().then(result => this.setState({ statekey: result }));
}
With the callBack function on the setState API you will fix your problem. In the link is the documentation of setState so you can see the setState and the arguments it accept.
I don't think you need a Async of Promise at this point as you see the onAuthStateChanged return a function, not a promise
ponentDidMount = () => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({user: user}, () => { this.props.retrieveMatches(this.state.user.uid);
})
}
});
}
本文标签: javascriptAsync await on component did mountStack Overflow
版权声明:本文标题:javascript - Async await on component did mount - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742208885a2433333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论