Last active
July 5, 2020 06:39
-
-
Save rehrumesh/6b367ba9876792625866209b5c6f0f01 to your computer and use it in GitHub Desktop.
Dogs App.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
import logo from "./logo.svg"; | |
import "./App.css"; | |
function App() { | |
const [dogImg, setDogImg] = useState(null); | |
const fetchDoggo = () => { | |
setDogImg(""); | |
fetch(`https://dog.ceo/api/breeds/image/random`) | |
.then((res) => res.json()) | |
.then((dogInfo) => { | |
setDogImg(dogInfo.message); | |
}); | |
}; | |
useEffect(() => { | |
if (dogImg === null) { | |
fetchDoggo(); | |
} | |
}); | |
return ( | |
<div> | |
<header> | |
<h3>Doggo of the day</h3> | |
<div> | |
<button onClick={() => fetchDoggo()}>New Doggo</button> | |
</div> | |
{dogImg !== "" ? ( | |
<div> | |
<img src={dogImg} width="400px" alt="doggo" /> | |
</div> | |
) : ( | |
<div>Loading Image</div> | |
)} | |
</header> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment