Created
May 28, 2020 07:53
-
-
Save SebinLee/7fd2aaf57c6a7b6ccc391d697ff23459 to your computer and use it in GitHub Desktop.
React Testing Recipe 번역 - Data Fecthing
This file contains 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, { useState, useEffect } from "react"; | |
export default function User(props) { | |
const [user, setUser] = useState(null); | |
//fecth를 이용하여 데이터를 호출합니다. | |
async function fetchUserData(id) { | |
const response = await fetch("/" + id); | |
setUser(await response.json()); | |
} | |
//props.id가 변경된 경우 fetchUserData를 호출합니다. | |
useEffect(() => { | |
fetchUserData(props.id); | |
}, [props.id]); | |
if (!user) { | |
return "loading..."; | |
} | |
return ( | |
<details> | |
<summary>{user.name}</summary> | |
<strong>{user.age}</strong> years old | |
<br /> | |
lives in {user.address} | |
</details> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment