I am trying to render data from an API using a FlatList.
I can't get anywhere. I have a lot of trouble filling in the DATA and renderItem fields.
Could you help me ?
Thank you :)
import React from "react";
import { Text, ActivityIndicator, FlatList, View } from "react-native";
import axios from "axios";
export default class Results extends React.Component {
constructor(props) {
super(props);
//console.log('state',this.props)
this.state = {
city: "Montpellier", //this.props.city,
report: null, // Données de l'API
};
this.fetchWeather();
}
fetchWeather() {
axios
.get(
"https://api.openweathermap.org/data/2.5/weather?q=" +
this.state.city +
"&appid=9fce19ee0d267aa48afdf331bb1668da",
)
.then((response) => {
this.setState({ report: response.data });
//console.log(response.data)
});
}
render() {
const DATA = this.state.report;
if (this.state.report === null) {
return <ActivityIndicator size="large" color="red" />;
} else {
return (
<FlatList data={DATA} renderItem={<Text> {this.state.report.id} </Text>} keyExtractor={(item) => item.id} />
);
}
}
}
renderItemshould be a function. It will have each item that is present in the data array. see reactnative.dev/docs/flatlist.html. You will be able to use{item.id}