Skip to content

Instantly share code, notes, and snippets.

@indreklasn
Last active August 29, 2022 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indreklasn/467001939065e9bac5d7fe462ecb2e77 to your computer and use it in GitHub Desktop.
Save indreklasn/467001939065e9bac5d7fe462ecb2e77 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({});
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