I am using the NASA API to grab pictures of Mars taken by the Mars Rover. Currently, my code works with fetching the pictures however, I want it to be more dynamic. My code:
import React, { useEffect, useState } from "react";
import NavBar from "./NavBar";
const apiKey = process.env.REACT_APP_NASA_API_KEY;
function MarsPhoto() {
const [data, setData] = useState('null')
const [solDay, setSolDay] = useState('')
const apiUrl = `https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=${solDay}&camera=fhaz&api_key=${apiKey}`
const fetchPhotoData = (event) => {
if(event.key === 'Enter') {
fetch(apiUrl)
.then((response) => response.json())
.then((data) => setData(data))
}
}
useEffect(() => {
fetchPhotoData()
}, [])
return(
<>
<NavBar/>
<div className="sol-search">
<input
value={solDay}
onChange={event => setSolDay(event.target.value)}
placeholder="Enter a Sol Day"
type="text"/>
</div>
<div className="image-fhaz">
<img src={data.photos ? (data.photos.length !== 0 ? data.photos["0"]["img_src"] : 'https://www.ncenet.com/wp-content/uploads/2020/04/No-image-found.jpg') : 'null'}
alt="Pitcure"/>
</div>
</>
)
}
export default MarsPhoto;
When I say I want it to be dynamic I want the user to enter a 'solDay' and the API will show the image based on that sol day. I already have the 'solDay' parameter in the API. I have used a key handler so when I enter the number it will make the API call and return the image. I have the input with the event, however, it is throwing an "MarsPhoto.js:14 Uncaught TypeError: Cannot read properties of undefined (reading 'key')", I am not sure why? Rather than using fetch, would it be better to use Axios? I would prefer to use fetch as I am more familiar with this. What can I do?