Skip to content

Instantly share code, notes, and snippets.

@brunormferreira
Created April 3, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunormferreira/6b0637e7c07b0a9c300af0159aad6b3c to your computer and use it in GitHub Desktop.
Save brunormferreira/6b0637e7c07b0a9c300af0159aad6b3c to your computer and use it in GitHub Desktop.
chuck norris jokes hook
import { useState, useEffect } from 'react'
export function useChuckNorris() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState(undefined)
const [data, setData] = useState(undefined)
useEffect(() => {
async function getJoke() {
setLoading(true)
const response = await fetch('https://api.chucknorris.io/jokes/random')
const { value } = await response.json().catch(error => setError(error))
setLoading(false)
setData(value)
}
getJoke()
}, [])
return { data, loading, error }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment