【发布时间】: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