2

I am setting state in the constructor like this - this is for text that says "Fetching data...":

this.state = {
      loading: true
    }

Then I have an async function like this, which will get some data.

componentDidMount() {
    this.fetchData().done()
  }

  async fetchData() {
    const response = await fetch(URL)
    const json = await response.json()
    const stars = json.stargazers_count
    this.setState({ stars })
  }

How do I know when I can set the isLoading state to false and hide the loading text and display the data? I seem to be missing some piece here!

Thanks Jordan

3
  • async/await is not part of ES7. Commented Oct 21, 2016 at 6:02
  • @FelixKling isn't it formally accepted as an ES7 proposal though? I'd say that's close enough. Commented Oct 21, 2016 at 6:06
  • @patrick: Proposals are not part of any release until they reach stage 4. ES7(ES2016) was released this year and does not contain async/await (ecma-international.org/ecma-262/7.0). It will be part of ES2017. Commented Oct 21, 2016 at 6:12

1 Answer 1

1

When you insert stars in your state, you can also set isLoading to false

this.setState({ stars, isLoading: false });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.