[英]Fetch data from API using React Native
我正在尝试从下面的 url 中获取数据,但是当我启动代码时,它返回错误: TypeError: undefined is not an object (evaluating 'res.data.hints')
,因此什么也没发生,我遵循了各种教程,他们似乎想出了这个代码。
状态
constructor(props) {
super(props);
this.updateSearch();
this.state = {
data: [],
};
}
功能
updateSearch = () => {
const url = `https://api.edamam.com/api/food-database/v2/parser?ingr=b&app_id=000&app_key=00000&page=20`;
fetch(url)
.then((res) => res.json())
.then((res) => {
this.setState({
data: res.data.hints
});
})
.catch(error => {
console.log("error : "+ error)
});
};
平单
<FlatList
data={this.state.data}
renderItem={({ item }) => (
<ListItem>
<TouchableOpacity>
<View>
<Text>{item.label}</Text>
<Text>{item.brand}</Text>
</View>
</TouchableOpacity>
</ListItem>
)}
keyExtractor={(item) => item.foodId}
/>
this.setState({
data: res.hints
});
试试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.