My app fetches a photo using NASA’s APOD API. The app works properly but I want to add a functionality where it fetches a new photo on click. How do I trigger the useEffect hook(?) on click?
const [apod, setApod] = useState(null)
useEffect(() => {
async function displayApod() {
const data = getApod()
data.then(res => {
if(res) {
setApod(res[0])
} else {
setApod(null)
}
})
.catch(err => console.log(err))
}
displayApod()
}, [])
const getApod = async() => {
const response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=${config.apiKey}&count=1`)
const data = await response.json()
return data
}
if (!apod) {
return <h1>no image to display</h1>
} else {
const { date, explanation, hdurl, title, url } = apod
return (
<div className='App'>
<div className="container mx-auto p-4 flex flex-col items-center">
<p>{title}</p>
<img src={url}/>
<p>{date}</p>
<p>{explanation}</p>
{/* Need to get new photo onClick */}
<button onClick={getApod}>New Photo</button>
</div>
</div>
);
}