代码之家  ›  专栏  ›  技术社区  ›  MadeInDreams

如何在本地应用程序中检索获取数据

  •  2
  • MadeInDreams  · 技术社区  · 6 年前

    我想知道如何在我的应用程序中查看我的fetch查询中的数据。

    我有一个从react native获取的节点,我想显示响应。

    节点部分;

    app.get('/balance', function(req, res){
        //calling the function
        res.jsonp('0');
          console.log("CRYPTO CALLED");
          });
    

    反应功能;

    _getBalanceFromApiAsync() {
     fetch('http://192.168.1.100:3000/ballance')
        .then(response => response.json())
        .then(responseJson => {
          console.log(responseJson);
    
          return responseJson;
        })
        .catch((error) => {
          console.error(error);
        });
    }
    

    我可以在节点控制台和反应控制台中看到结果。但不起作用的地方就在这里。

    本地应用程序;

    <Text style={styles.getStartedText}>Your Wallet Balance</Text>
              <Text> {this._getBalanceFromApiAsync()}</Text>
               </View>
    

    函数正在执行,但我想显示返回的值,因为文本字段是空的

    谢谢你

    1 回复  |  直到 6 年前
        1
  •  2
  •   Haider Ali    6 年前

    很简单,您需要设置重新呈现组件的状态。尝试这样做

    constructor(props){
       super(props)
    this.state = {
        textData: ''
    }
    }
    
    componentDidMount(){
    this.getBalanceFromApiAsync()
    }
    
    getBalanceFromApiAsync() {
     fetch('http://192.168.1.100:3000/ballance')
        .then(response => response.json())
        .then(responseJson => {
          console.log(responseJson);
           this.setState({
          textData: responseJson
    })
        })
        .catch((error) => {
          console.error(error);
        });
    }
    
    <Text style={styles.getStartedText}>Your Wallet Balance</Text>
              <Text> {this.state.textData}</Text>
               </View>