本文介绍了为什么取返回整数的一个奇怪的哈希 - 第2部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用异步/的await 与本地做出反应。

response.json我的结果()是:
{_45:0,_81:0,_65:空,_54:空}

无论出于何种原因,我想实际的响应位于_65,我不知道这些随机密钥。

看来,它关系到一个事实,即以.json()返回一个承诺。

  componentDidMount(){
    this.getData(),那么(数据=> this.setState({}数据))。
  }
  异步的getData(){
    尝试{
      让我们响应=的await取(myUrl)
      让JSON =等待response.json()
      返回JSON
    }赶上(ERR){
      Alert.alert(NULL,ERR)
    }
  }
  渲染(){
    常量{}数据= this.state
  ...


解决方案

答案是:this.getData(),然后(数据=> this.setState({}数据))

I'm using async/await with React Native.

My result from response.json() is:{ _45: 0, _81: 0, _65: null, _54: null }

For whatever reason, the actual response I want is located in _65 and I have no idea what these random keys are.

It seems that it is related to the fact that .json() returns a Promise.

  componentDidMount() {
    this.getData().then(data => this.setState({ data }))
  }
  async getData() {
    try {
      let response = await fetch(myUrl)
      let json = await response.json()
      return json
    } catch(err) {
      Alert.alert(null, err)
    }
  }
  render() {
    const { data } = this.state
  ...
解决方案

The answer is: this.getData().then(data => this.setState({ data }))

这篇关于为什么取返回整数的一个奇怪的哈希 - 第2部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 15:47
查看更多