I want to display the NASA APOD API data in React. I’ve done it in vanilla JS but I’m having issues doing it in React. When I console log the data, I can see the array of information from the NASA API. However when I try to return anything, it says undefined. I’ve tried photoData.date, data.date, but all are undefined yet I can see the array.
import React, { useState, useEffect } from "react";
const count = 6;
const apiKey = 'myapikey'
const apiURL = `https://api.nasa.gov/planetary/apod?api_key=${apiKey}&count=${count}`;
function Card() {
const [photoData, setPhotoData] = useState(null);
useEffect(() => {
fetchPhoto();
async function fetchPhoto() {
const res = await fetch(apiURL);
const data = await res.json();
setPhotoData(data);
console.log(data);
}
}, []);
return (
<React.Fragment>
<div>
<h1>{photoData.date}</h1>
</div>
</React.Fragment>
);
};
export default Card;
What am I missing here?