
4์ฃผ์ฐจ์ mock data ํ์ฉํ์ฌ react westagram์ ๊ตฌํํ๊ธฐ ์ํด ๊ณต๋ถํ๋ ์ค
fetch() ํจ์ ์ฌ์ฉ๋ฒ์ ๋ํด ๋ ๊ณต๋ถํ๊ณ ์ ์ฐพ์๋ณด๊ณ  ์ ๋ฆฌํ ๋ด์ฉ ๊ธฐ๋กํด๋ณธ๋ค.
โจmock data
โ mock data๋? mock data๊ฐ ํ์ํ ์ด์
- UI๋ฅผ ๊ตฌํํ๋ค ๋ณด๋ฉด API๊ฐ ๋์ค๊ธฐ ์ ์ ํ์ด์ง๊ฐ ๋จผ์  ๋์ค๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ,
- ์ด ๋ ํ๋ก ํธ์๋ ๊ฐ๋ฐ์๋ API๊ฐ ๋์ค๊ธฐ๋ง์ ๊ธฐ๋ค๋ฆฌ๋๊ฒ ์๋ mock data ์ฆ, ๊ฐ์ง ๋ฐ์ดํฐ? ์ํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด์
- ๋ด๊ฐ ๋ง๋ ํ๋ฉด์์ ๋ฐ์ดํฐ๊ฐ ์ด๋ป๊ฒ ๋ํ๋๋์ง๋ฅผ ๋ฏธ๋ฆฌ ํ ์คํธํ๋ฉฐ ๊ฐ๋ฐ์ ์งํํ๋ค
- mock data๋ฅผ ์ ๋ง๋ค์ด ๋๋ค๋ฉด, ๋ฐฑ์๋์ ์ค์  ์ฐ๊ฒฐํ ๋๋ ์์ํ๊ฒ ์์ ํ ์ ์์ ๋ฟ๋ง ์๋๋ผ
- ์๋ฃ์ ํํ๊ฐ ๊ตฌ์ฒด์ ์ผ๋ก ํน์ ๋๊ธฐ ๋๋ฌธ์ ์ํต์๋ ๋์์ด ๋๋ค
โ mock data๋ฅผ ๊ด๋ฆฌํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ
1. .js ํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒ
- .jsํ์ผ์ ์ด๋ค ๋ฐ์ดํฐ์ธ์ง ์ ์ ์๊ฒ ๋ช ๋ช ํ๋ค
- ๋ฐฐ์ด ๋ฐ์ดํฐ๋ฅผ ๋ณ์์ ๋ด๊ณ  ํ์ํ component์ importํด์ ์ฌ์ฉ
- .jsํ์ผ์ ๋ฐ์ดํฐ๋ฅผ- importํ๋ component ๋ฐ๋ก ์์ผ๋ก ์ ๊ทผํ๊ธฐ ์ฌ์ด ๊ณณ์ ์์ฑํ๋ค
const COMMENT = [
  {
    id: 1,
    userName: "wecode",
    content: "Welcome to world best coding bootcamp!",
    isLiked: true,
  },
  {
    id: 2,
    userName: "yurim45",
    content: "Hi there.",
    isLiked: false,
  },
  {
    id: 3,
    userName: "april_5",
    content: "Hey.",
    isLiked: false,
  },
];
export default COMMENT;
2. โญ.json ํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒโญ
- ๋ ๋ฒ์งธ๋ ์ค์  API์์ ๋ณด๋ด์ฃผ๋ ๋ฐ์ดํฐ ํ์์ ๋ง๊ฒ .jsonํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ๋ด์fetchํจ์๋ฅผ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ๋ฐฉ๋ฒ
- ์์น publicํด๋ >dataํด๋ >.json
- ํด๋น ๋ฐ์ดํฐ๋ http://localhost:3000/data/ํ์ผ๋ช .json์์ ํ์ธ ๊ฐ๋ฅ
[
  {
    id: 1,
    userName: "wecode",
    content: "Welcome to world best coding bootcamp!",
    isLiked: true,
  },
  {
    id: 2,
    userName: "yurim45",
    content: "Hi there.",
    isLiked: false,
  },
  {
    id: 3,
    userName: "april_5",
    content: "Hey.",
    isLiked: false,
  },
];
๐ fetch() ํจ์
- ๋ฐฑ์๋๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ค๋ฉด API๋ฅผ ํธ์ถํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์๋ต๋ฐ๋๋ค
- ์ด ๋ ์๋ฐ์คํฌ๋ฆฝํธ Web API fetch()ํจ์๋ฅผ ์ฐ๊ฑฐ๋axios library๋ฅผ ์ฌ์ฉํ๋ค
- API๋  library๋  ์ด๋๊ฒ์ ์ฌ์ฉํด๋ ๊ด์ฐฎ์ง๋ง, ์ค์ํ ๊ฒ์
    - http ํต์ ์ ์์ฒญ๊ณผ ์๋ต์ ๋ํ ์ดํด์ โจ
- promise ๊ฐ๋ ์ ๋ํ ๊ณต๋ถ๊ฐ ๋ ์ค์ํ๋คโจ
 
โ fetch() ํจ์ ๊ธฐ๋ณธ
โ ํ์ดํ ํจ์ ์ ์ธ
fetch("api ์ฃผ์")
  .then((res) => res.json())
  .then((res) => {
    // data๋ฅผ ์๋ต ๋ฐ์ ํ์ ๋ก์ง
  });
โ ES5์ ํจ์ ์ ์ธ
fetch("api ์ฃผ์")
  .then(function (res) {
    return res.json();
  })
  .then(function (res) {
    // data๋ฅผ ์๋ต ๋ฐ์ ํ์ ๋ก์ง
  });
โ fetch() ํจ์ - method๊ฐ GET์ธ ๊ฒฝ์ฐ
- fetch() ํจ์์์ default method๋ GET ์ด๋ค
โ๏ธ API ๋ช ์ธ
์ค๋ช
: ์ ์  ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
base url: https://api.google.com
endpoint: /user/3
method: get
์๋ตํํ:
    {
        "success": boolean,
        "user": {
            "name": string,
            "batch": number
        }
    }
โ๏ธ APIํธ์ถ
fetch('https://api.google.com/user/3')
  .then(res => res.json())
  .then(res => {
    if (res.success) {
        console.log(`${res.user.name}` ๋ ํ์ํฉ๋๋ค);
    }
  });
โ๏ธ ์์
import React, { Component } from 'react';
class User extends Component {
  componentDidMount() {
    // user id๊ฐ props๋ฅผ ํตํด ๋์ด์จ๋ค๊ณ  ๊ฐ์ 
    const { userId } = this.props;
    fetch(`https://api.google.com/user/${userId}`)
      .then(res => res.json())
      .then(res => {
        if (res.success) {
            console.log(`${res.user.name}` ๋ ํ์ํฉ๋๋ค);
        }
      });
  }
}
โ fetch() ํจ์ - method๊ฐ POST์ธ ๊ฒฝ์ฐ
- POST์ธ ๊ฒฝ์ฐ์๋ fetch() ํจ์์ method ์ ๋ณด๋ฅผ ์ธ์๋ก ๋๊ฒจ์ฃผ์ด์ผ ํ๋ค
โ๏ธ API ๋ช ์ธ
์ค๋ช
: ์ ์ ๋ฅผ ์ ์ฅํ๋ค.
base url: https://api.google.com
endpoint: /user
method: post
์์ฒญ body:
    {
        "name": string,
        "batch": number
    }
์๋ต body:
    {
        "success": boolean
    }
โ๏ธ APIํธ์ถ
fetch("https://api.google.com/user", {
  method: "post",
  body: JSON.stringify({
    name: "april",
    batch: 1,
  }),
})
  .then((res) => res.json())
  .then((res) => {
    if (res.success) {
      alert("์ ์ฅ ์๋ฃ");
    }
  });
- ๋ ๋ฒ์งธ ์ธ์์ method์ body๋ฅผ ๋ณด๋ด์ฃผ์ด์ผ ํ๋ค
- method๋ post
- body๋ JSONํํ๋ก ๋ณด๋ด๊ธฐ ์ํด JSON.stringfy()ํจ์์ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌํ์ฌ JSONํํ๋ก ๋ณํํ๋ค.
โfetch() ํจ์ - method๊ฐ GET์ธ๋ฐ parameter๋ฅผ ์ ๋ฌํด์ผ ํ๋ ๊ฒฝ์ฐ
- path๋ง๊ณ- query string์ผ๋ก ๋๊ฒจ์ค์ผ ํ ์๋ ์๋ค
โ๏ธ API ๋ช ์ธ
์ค๋ช
: ์ ์  ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
base url: https://api.google.com
endpoint: /user
method: get
query string: ?id=์์ด๋
์๋ตํํ:
    {
        "success": boolean,
        "user": {
            "name": string,
            "batch": number
        }
    }
โ๏ธ APIํธ์ถ
fetch('https://api.google.com/user?id=3')
  .then(res => res.json())
  .then(res => {
    if (res.success) {
        console.log(`${res.user.name}` ๋ ํ์ํฉ๋๋ค);
    }
  });
โ res.json()์ ์๋ฏธ
    fetch('http://localhost:3000/data/feedData.json')
      .then(res => {
        console.log(res);
        return res.json();
      })
      .then(data => {
        console.log(data);
        this.setState({
          feedData: data,
        });
      });
  }
์ค์  ์์ฑํ ์ฝ๋๋ก ์ฒซ๋ฒ์งธ .then'์ res์ ๋๋ฒ์งธ .thenโ์ data๋ฅผ console.log์์ ํ์ธํด๋ณด์๋ค
- 
    ์ฒซ๋ฒ์งธ .then'์res`- http ํต์  ์์ฒญ๊ณผ ์๋ต์์ ์๋ต์ ์ ๋ณด๋ฅผ ๋ด๊ณ  ์๋ ๊ฐ์ฒด Response Object
- ์ค์  ๋ฐ์ดํฐ๋ ๋ณด์ด์ง ์๊ธฐ ๋๋ฌธ์
- ์๋ต์ผ๋ก ๋ฐ๋ JSON ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ Response Object ์ json ํจ์๋ฅผ ํธ์ถํ๊ณ , return ํด์ผํ๋ค
 
- http ํต์  ์์ฒญ๊ณผ ์๋ต์์ ์๋ต์ ์ ๋ณด๋ฅผ ๋ด๊ณ  ์๋ ๊ฐ์ฒด 
- 
    ๋๋ฒ์งธ .then'์data`- ์ฒซ๋ฒ์งธ .thenํจ์์์ ์๋ต body์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ ์๋ค
 
- ์ฒซ๋ฒ์งธ 
โ ๋ฐฑ์ค๋์์ ์๋ต body๋ฅผ ์ ์ฃผ๋ ๊ฒฝ์ฐ
- res.status์๋ต ์ฝ๋๋ก ํ์ธํ๋ฉด ๋๋ค- 200 ์ ์
- 403 ๋ฐฑ์๋ ์ฐพ์??
 
ํ์ฌ๊น์ง react๋ก ์์
ํ๊ณ  mock data๋ก ํ
์คํธ๊น์ง ํด๋ณธ westagram project
์์ง ๊ฐ ๊ธธ์ด ๋ฉ์ง๋ง~ ๋๋ฆ ๋ฟ๋ฏ??ใ
ใ
 ์ผํฌ์ผ๋น ์คโฆ๐
