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("์ €์žฅ ์™„๋ฃŒ");
    }
  });


  1. ๋‘ ๋ฒˆ์งธ ์ธ์ž์— method์™€ body๋ฅผ ๋ณด๋‚ด์ฃผ์–ด์•ผ ํ•œ๋‹ค
  2. method๋Š” post
  3. 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 ํ•ด์•ผํ•œ๋‹ค
  • ๋‘๋ฒˆ์งธ .then'์˜ data`

    • ์ฒซ๋ฒˆ์งธ .then ํ•จ์ˆ˜์—์„œ ์‘๋‹ต body์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค


โ—‹ ๋ฐฑ์•ค๋“œ์—์„œ ์‘๋‹ต body๋ฅผ ์•ˆ ์ฃผ๋Š” ๊ฒฝ์šฐ

  • res.status ์‘๋‹ต ์ฝ”๋“œ๋กœ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค
    • 200 ์ •์ƒ
    • 403 ๋ฐฑ์—”๋“œ ์ฐพ์ž??



ํ˜„์žฌ๊นŒ์ง€ react๋กœ ์ž‘์—…ํ•˜๊ณ  mock data๋กœ ํ…Œ์ŠคํŠธ๊นŒ์ง€ ํ•ด๋ณธ westagram project ์•„์ง ๊ฐˆ ๊ธธ์ด ๋ฉ€์ง€๋งŒ~ ๋‚˜๋ฆ„ ๋ฟŒ๋“ฏ??ใ…‹ใ…‹ ์ผํฌ์ผ๋น„ ์ค‘โ€ฆ๐Ÿ˜…


์˜ˆ๋ฆฌ๋‹˜์˜ ๋ธ”๋กœ๊ทธ