Camper Leaderboard Async Await

Camper Leaderboard Async Await
0.0 0

#1

I tried to use async await in React for the first time. Just looking for feedback on whether this is the best way to implement it or if there is a better way?
Thanks in advance


#2

You don’t need to async/await componentDidMount(). Just use it as usual:

componentDidMount(){
  this.getData();
}

And async/avait should be wrapped in try/catch block:

this.getData = async () => {
  try {
    const res = await fetch(
      this.state.recent ? this.state.last30 : this.state.allTime
    );
    const json = await res.json();
    this.setState({ records: json }, () => console.log('done'));
  } catch (e) {
    // handle error
    console.error(e);
  }
};

#3

Thanks man, missed the componentDidMount() when I refactored.