On every call of the API I get a new image link (https://dog.ceo/api/breeds/image/random this is the API I am calling), I am using useEffect hook and calling the API.
Every time I click the button I want to hit the API, get the new value, and re-render the image.
When I pass "dog" as a dependency to useEffect, I go into an infinite loop, all I want is when I click on the button I want to update the image.
const [dog, setDog] = useState('')
useEffect(() => {
const getDogs = async () => {
const dogsFromServer = await fetchDogs()
setDog(dogsFromServer.message)
}
getDogs()
}, [])
const fetchDogs = async () => {
const res = await fetch('https://dog.ceo/api/breeds/image/random')
const data = await res.json()
console.log(data.message)
return data
}
const changeDog = ()=> {
alert("changeddog")
}
return (
<div className="App">
<img src={dog}/>
<button onClick={()=>fetchDogs()}>Change Image<button/>
</div>
);