Hi i am trying to show the data from a json url in ReactJS by inputing all the data to an array variable but i can't use the array in the JSX part because the array is not yet populated while rendering i try'd many things but i always end up in a promise loop where i need a promise to get the data from the other one. THE CODE:
let arry = [];
let ar = [];
async function getdriver() {
const response = await fetch("https://ergast.com/api/f1/current/drivers.json");
ar = await response.json();
ar.MRData.DriverTable.Drivers.forEach((element) => {
arry.push(element);
});
return arry;
}
getdriver();
console.log(arry);// the array is populated but i think it waits for it before showing
console.log(arry.lenght); //lenght is 0
JSX:
return (
<div>
<Menu />
<div style={{ textAlign: "left" }}>
<h4>ff</h4>
<Button >change</Button>
<br></br>
<i>{arry[0].code}</i>// error ' Cannot read property 'code' of undefined ' so arry is empty?
</div>
</div>
);