Skip to content

Instantly share code, notes, and snippets.

@indreklasn
Last active August 29, 2022 13:59
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 indreklasn/7a24038194a4146f8cbc4a959d4d97b1 to your computer and use it in GitHub Desktop.
Save indreklasn/7a24038194a4146f8cbc4a959d4d97b1 to your computer and use it in GitHub Desktop.
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