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 badges
Add a ment  | 

4 Answers 4

Reset to default 4

you 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