Skip to content

Instantly share code, notes, and snippets.

@simicd
Last active September 27, 2020 17:26
Show Gist options
  • Save simicd/37306ff167f875b0227b57ba9c9fb911 to your computer and use it in GitHub Desktop.
Save simicd/37306ff167f875b0227b57ba9c9fb911 to your computer and use it in GitHub Desktop.
// DogImageWithButton.tsx
import React, { FC } from "react";
import { useFetch } from "./useFetch";
type DogImageType = { message: string; status: string };
export const DogImageWithButton: FC = () => {
/** Fetch image on button click */
const { data, fetchApi } = useFetch<DogImageType>({
url: "https://dog.ceo/api/breed/beagle/images/random",
});
const getImage = () => {
fetchApi();
};
return (
<>
{data ? <img src={data.message} alt="dog"></img> : <div>Loading</div>}
<button onClick={() => getImage()}>New Image</button>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment