Created
February 22, 2021 04:15
-
-
Save rohanBagchi/768301c7efb6a536158682104147abed to your computer and use it in GitHub Desktop.
devto-testing gist
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 "./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