Last active
November 6, 2021 22:35
-
-
Save szaranger/270a46feacac810077fc53f1448baf5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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, { 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