Last active
August 29, 2022 13:58
-
-
Save indreklasn/467001939065e9bac5d7fe462ecb2e77 to your computer and use it in GitHub Desktop.
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 React, { useState, useEffect } from "react"; | |
const Planets = () => { | |
const [hasError, setErrors] = useState(false); | |
const [planets, setPlanets] = useState({}); | |
useEffect(() => { | |
async function fetchData() { | |
const res = await fetch("https://swapi.co/api/planets/4/"); | |
res | |
.json() | |
.then(res => setPlanets(res)) | |
.catch(err => setErrors(err)); | |
} | |
fetchData(); | |
}, [); | |
return ( | |
<div> | |
<span>{JSON.stringify(planets)}</span> | |
<hr /> | |
<span>Has error: {JSON.stringify(hasError)}</span> | |
</div> | |
); | |
}; | |
export default Planets; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment