I can't access the data stored in my sessionDates array in my DateList function
this is the code from my index.js including the api call
import React from "react";
import ReactDOM from "react-dom";
const availableDates = [];
const available = [];
const sessionDates = [];
function getDates(e){
getDate(apiUrl);
}
const getDate = async (apiUrl)=>{
const res = await fetch(apiUrl)
try {
const data = await res.json();
availableDates.push(data);
availableDates.map((datee) => {
datee.map((session) => {
sessionDates.push(session.date.slice(0, 10));
});
});
return sessionDates;
} catch(error) {
console.log("error", error);
}
}
window.addEventListener('load', getDates);
function DateList(){
return <section>{sessionDates[0]}</section>;
}
While you are setting sessionDates based on the api call, there is nothing in your code that prompts DateList to re-render when sessionDates changes. As such, it will only display the initial value of sessionDates, for which sessionDates[0] is undefined.
Instead, I recommend that you use the useEffect hook to trigger your api call and implement sessionDates using useState.