Skip to content

Instantly share code, notes, and snippets.

@rehrumesh
Last active July 5, 2020 06:39
Show Gist options
  • Save rehrumesh/6b367ba9876792625866209b5c6f0f01 to your computer and use it in GitHub Desktop.
Save rehrumesh/6b367ba9876792625866209b5c6f0f01 to your computer and use it in GitHub Desktop.
Dogs App.js
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