Programmer Jokes {`#${data[0].id}`}
{data[0].setup}
{data[0].punchline}
import React, { useEffect, useState } from "react"; /** * This component is generated as an example for fetch */ const API_URL = "https://official-joke-api.appspot.com/jokes/programming/random"; export const FetchExample = () => { const [error, setError] = useState(null); const [isLoaded, setIsLoaded] = useState(false); const [data, setData] = useState([]); console.log("data: ", data); // Note: the empty deps array [] means // this useEffect will run once // similar to componentDidMount() useEffect(() => { fetch(API_URL) .then((res) => { console.log("res: ", res); return res.json(); }) .then( (result) => { console.log("result: ", result); setData(result); setIsLoaded(true); }, // Note: it's important to handle errors here // instead of a catch() block so that we don't swallow // exceptions from actual bugs in components. (error) => { setIsLoaded(true); setError(error); }, ); }, []); if (error) { return
Programmer Jokes {`#${data[0].id}`}
{data[0].setup}
{data[0].punchline}