Skip to content

Instantly share code, notes, and snippets.

@snigo
Last active February 22, 2024 22:20
Show Gist options
  • Save snigo/b7dff51120a34d5cd43be503f4c3bf9e to your computer and use it in GitHub Desktop.
Save snigo/b7dff51120a34d5cd43be503f4c3bf9e to your computer and use it in GitHub Desktop.
Quote of the Day v1.0.0
function QuoteOfTheDay() {
const [quote, setQuote] = useState("");
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch("https://api.quotable.io/random")
.then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch quote");
}
return response.json();
})
.then((data) => {
setQuote(data.content);
setIsLoading(false);
})
.catch((error) => {
setError(error.message);
setIsLoading(false);
});
}, []);
return (
<main className={styles.main}>
<h1>Quote of the Day</h1>
{isLoading ? (
<p>Loading...</p>
) : error ? (
<p>Error: {error}</p>
) : (
<blockquote>
<p>{quote}</p>
</blockquote>
)}
</main>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment