Skip to content

Instantly share code, notes, and snippets.

@szaranger
Last active November 6, 2021 22:35
Show Gist options
  • Save szaranger/270a46feacac810077fc53f1448baf5f to your computer and use it in GitHub Desktop.
Save szaranger/270a46feacac810077fc53f1448baf5f to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from "react";
function Character({ id }) {
/** Move this! */
// if (!id) {
// return "Please select a game to fetch";
// }
const [character, setCharacter] = useState({
name: "",
description: ""
});
const fetchCharacter = async () => {
const response = await fetch(`https://swapi.dev/api/people/${id}`);
const data = await response.json();
console.log("here");
setCharacter(data);
};
useEffect(() => {
if (id) {
fetchCharacter();
}
}, [id]);
/** to here */
if (!id) {
return "Please select a game to fetch";
}
return (
<main>
<h2>{character.name}</h2>
<div>Name: {character.birth_year}</div>
<div>Height: {character.height}</div>
</main>
);
}
export default Character;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment