Last active
August 29, 2022 13:59
-
-
Save indreklasn/7a24038194a4146f8cbc4a959d4d97b1 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({}); | |
async function fetchData() { | |
const res = await fetch("https://swapi.co/api/planets/4/"); | |
res | |
.json() | |
.then(res => setPlanets(res)) | |
.catch(err => setErrors(err)); | |
} | |
useEffect(() => { | |
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