【问题标题】:React Native: Fetch api call is returning inappropriate responseReact Native:Fetch api调用返回不适当的响应
【发布时间】:2017-02-19 17:48:54
【问题描述】:

我正在使用 fetch 对服务器进行 api 调用,并且响应 json 文件正在返回未定义类型的响应。我想记录响应并将其呈现在屏幕上。

我的源代码:

'use strict';

import React, {Component} from 'react';
import {StyleSheet, View, Text, ListView, ScrollView} from 'react-native';

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

class BusList extends Component {

    constructor() {
        super();
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
        this.state = {
            dataSource: ds.cloneWithRows(['row 1', 'row 2']),
        };
    }

    componentDidMount() {
        this.loadJSONData();
    }

    loadJSONData() {
        fetch('my_api!')
      .then((response) => {
        return response.json()
      })
      .then((responseJSON) => {
          return this.setState({dataSource: this.state.dataSource.cloneWithRows(responseJSON)});
      })
      .catch((error) => {
        console.warn(error);
      });
}

renderRow(rowData) {
    return(
        <View>
            <Text>{rowData.data}</Text>
        </View>
    );
}

render() {
return (
  <View>
    <ListView
      dataSource={this.state.dataSource}
      renderRow={(rowData) => <Text>{rowData}</Text>}
    />
  </View>
);
  }
}

module.exports = BusList;

但是,当我尝试记录它时,我得到以下log

请帮帮我!

【问题讨论】:

    标签: javascript json react-native fetch


    【解决方案1】:

    我认为不使用任何包装器将其转换为 json 对象会使它变得非常复杂。你可以这样做:

    //create state 
    constructor() {
            super();
            const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
            this.state = {
                dataSource: ds.cloneWithRows([]),
                getData:[]
            };
        } 
    
     loadJSONData() {
            fetch('my_api!', {
                method: 'get',
                dataType: 'json',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                }
        })
          .then((response) => {
            this.setState({getData:response})
    //putting response inside listview directly or you can put this.state.getData 
            this.setState({dataSource:ds.cloneWithRows(response)})
          })
          .catch((error) => {
            console.warn(error);
          });
    

    干杯:)

    【讨论】:

    • 非常感谢,这可行,但这里的问题是处理 json 对象!好的,请简要说明处理高度嵌套的json对象,或者任何我可以帮助我更合适地实现rowHasChanged函数的链接!
    • 每当数据源发生变化时,它将重新渲染,并且您的 listView 的 renderRow api 将触发以更新列表视图
    • 在您的 renderRow() 中,您将采用参数 renderRow(rowData) ,之后您可以使用“。”简化您的 json 数据。运算符,例如,如果您有一些键名详细信息,并且在该详细信息键中还有更多键,即“id”、“地址”等,您可以通过 rowData.detail.address 获取每行的“地址”。 :)
    • Invariant Violation:对象作为 React 子级无效(发现:带有键 {map} 的对象)。如果您打算渲染一组子项,请改用数组或使用 React 附加组件中的 createFragment(object) 包装对象。检查Text的渲染方法。这是我得到的日志!
    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2018-04-14
    • 2023-03-19
    • 1970-01-01
    • 2014-04-30
    • 2019-12-09
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多