【发布时间】:2016-10-26 10:06:52
【问题描述】:
我正在尝试使用以下代码 sn-p 在本机反应中使用 fetch api。每当我尝试 console.log API 数据数组时,它总是出现“未定义”?!?!
Home.JS
import api from '../components/api';
class Home extends Component {
constructor(props){
super(props);
this.state = {
profile: []
}
}
componentWillMount(){
api.getProfile().then((res) => {
this.setState({
profile: res.profile
})
});
}
render() {
console.log("Profile: ", this.state.profile); // Console Out API Data
return (
<View style={styles.container}>
<Text style={styles.welcome}></Text>
</View>
);
}
};
API.JS
var api = {
getProfile(){
var url = 'https://api.lootbox.eu/xbl/us/Food%20Market/profile';
return fetch(url).then((res) => res.json());
}
};
module.exports = api;
【问题讨论】:
-
尝试使用
console.logres.data而不是res.profile -
这个解决方案不走运:/
标签: javascript react-native fetch react-native-android