Skip to content

Instantly share code, notes, and snippets.

@SebinLee
Created May 28, 2020 07:53
Show Gist options
  • Save SebinLee/7fd2aaf57c6a7b6ccc391d697ff23459 to your computer and use it in GitHub Desktop.
Save SebinLee/7fd2aaf57c6a7b6ccc391d697ff23459 to your computer and use it in GitHub Desktop.
React Testing Recipe 번역 - Data Fecthing
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