Skip to content

Instantly share code, notes, and snippets.

@rohanBagchi
Created February 22, 2021 04:15
Show Gist options
  • Save rohanBagchi/768301c7efb6a536158682104147abed to your computer and use it in GitHub Desktop.
Save rohanBagchi/768301c7efb6a536158682104147abed to your computer and use it in GitHub Desktop.
devto-testing gist
import "./styles.css";
import { useState } from "react";
import { get } from "axios";
export default function App() {
const [joke, setJoke] = useState(null);
const [error, setError] = useState(null);
const fetchJoke = async () => {
try {
const { data } = await get("https://api.icndb.com/jokes/random");
if (data.type === "success") {
setJoke(data?.value?.joke);
setError(null);
}
} catch (e) {
setError("Fetch failed. Please retry!");
}
};
const renderJoke = () => {
if (error) {
return <h3>{error}</h3>;
}
return <h3>{joke}</h3>;
};
return (
<div className="App">
<button onClick={fetchJoke}>Get a random joke</button>
{renderJoke()}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment