如何在异步函数循环后设置状态

我能找到的最接近的答案是这个,这没有帮助,因为我需要 setState: How do I run a function after using array.map?


我认为答案应该很简单,但我对 Javascript 还很陌生。我正在尝试将 setState 移动isLoading到我拉完所有配置文件之后。


componentDidMount() {

    console.log('MatchesScreen init props: ', Object.keys(this.props))


    Object.keys(this.props.profile.profile.matches).map((username) => {

      console.log('match', username)

      url = 'https://xxxxxxx.execute-api.us-west-2.amazonaws.com/prod/profile?username=' + username

      fetch(url, {

        method: 'GET',

      })

      .then((response) => response.json())

      .then((responseJson) => {

        this.setState({

          isLoading: false,

        })

        this.props.addMatch(responseJson)

      })

      .catch((error) =>{

        console.error(error);

      })

    })

  }

我尝试了各种方法,例如将 a 附加.then()到 map 函数,但到目前为止还没有运气。


守着星空守着你
浏览 182回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript