I'm trying to map through an Array of Objects, every object is fetched from a folder of JSONs in my project. then i want to Render each object as a react component. to achieve this i set the state of my component to that array of object.
class List extends React.Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
when i console.log() my state it seems to be what i needed, but when i try to console.log() the first element of that array it is undefined. this is the console.log(this.state.data)
[]
0: {description: "AirEuropa Boarding Pass", organizationName: "Air Europa", passTypeIdentifier: "pass.com.globalia.aireuropa.boardingpass", serialNumber: "RB47ZP20082C640001010191641560895200000", teamIdentifier: "ELASV6M68G", …}
1: {formatVersion: 1, passTypeIdentifier: "pass.com.renfe-RenfeTicket", serialNumber: "00HHGV7F7353900982276", teamIdentifier: "H9VY2F2XZA", webServiceURL: "https://www.myserver.com/pathto/service", …}
length: 2
__proto__: Array(0)
why is this.state.data[0] undefined?
This is how i get data from JSONs
const arr = [];
axios
.get("data")
.then(res => {
const arrayDatas = res.data;
arrayDatas.forEach(element => {
if (element.includes(".json")) {
axios.get("data/" + element).then(res => {
arr.push(res.data);
});
}
});
this.setState({
data: arr
});
})
.catch(error => console.error(error));
}