Skip to content

Instantly share code, notes, and snippets.

@barhoring
Forked from indreklasn/Planets.js
Created March 17, 2020 10:50
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 barhoring/b66858db661d5c3baceb0846e58eb696 to your computer and use it in GitHub Desktop.
Save barhoring/b66858db661d5c3baceb0846e58eb696 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