๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[React] useEffect๋ฅผ ํ†ตํ•ด openAPI ๊ฐ€์ ธ์˜ค๊ธฐ (Fetch, Axios)(2023.05.26) ๋ณธ๋ฌธ

full stack/React

[React] useEffect๋ฅผ ํ†ตํ•ด openAPI ๊ฐ€์ ธ์˜ค๊ธฐ (Fetch, Axios)(2023.05.26)

Shinjio 2023. 5. 29. 17:52

๐ŸŽˆ ์˜ํ™” API ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

- fetch, Axios ์‚ฌ์šฉ

 

 

import React, { useEffect, useState } from "react";
import axios from "axios";
import "./App.css";

const Ex04 = () => {
  let movieUrl =
    "http://kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=f5eef3421c602c6cb7ea224104795888&targetDt=20230501";

  /**
   * ํ™”๋ฉด์— ์˜ํ™” ๋žญํ‚น์„ ๋„์–ด์ฃผ์ž!
   *
   * jQuery + Ajax >>>>> ๋…ผ์™ธ! (์‚ฌ์šฉ์ถ”์ฒœX ์†๋„ ๋Š๋ฆผ)
   * Fecth API (New!)
   * >> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” API
   * >> ๋ณ„๋„์˜ ์„ค์น˜๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Œ
   * Axios (New!) >>>>> ๋Œ€๋ถ€๋ถ„ react์—์„œ ์„ ํ˜ธํ•จ
   *
   * ๊ณตํ†ต์  : ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” API
   */

  /* ์™œ API๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๊ฒƒ์„ useEffect ์•ˆ์— ๋„ฃ์–ด์ค„๊นŒ?
    - useEffect์˜ ์ˆœ์„œ๋Š” ํ™”๋ฉด์ด ๋งˆ์šดํŠธ ๋œ ํ›„!
    - ํ™”๋ฉด์ด ๋จผ์ € ๋œจ๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๋Ÿฌ ๊ฐ
    - ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ ์กฐ๊ธˆ ๋” ๋น ๋ฅธ ํ™˜๊ฒฝ์„ ์ œ๊ณต
*/

  const [list, setList] = useState([]);

  useEffect(() => {
    // Case 1) Fetch API ์‚ฌ์šฉ
    // fetch(movieUrl)
    //   .then((res) => res.json()) //๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ json ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜
    //   .then((res) => {
    //     console.log("fetch res:", res.boxOfficeResult.dailyBoxOfficeList);
    //     //Q.fetch API๋กœ ๊ฐ€์ง€๊ณ  ์˜จ ๊ฐ’์„ 1~10์œ„๊นŒ์ง€ ํ™”๋ฉด์— ๋„์›Œ์ฃผ์ž
    //     //  ์ˆœ์œ„. ์˜ํ™”์ œ๋ชฉ - ๊ฐœ๋ด‰์ผ์ž
    //     setList(res.boxOfficeResult.dailyBoxOfficeList);
    //   })
    //   .catch(() => {
    //     console.error("error!");
    //   });
    /**
     * Case 2) Axios ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
     * (1) axios ์„ค์น˜ - npm install axios >> npm i axios
     * (2) import axios from 'axios'
     *
     */

    axios
      .get(movieUrl)
      .then((res) => {
        console.log("Axios res :", res.data.boxOfficeResult.dailyBoxOfficeList); //json์œผ๋กœ ๋„˜์–ด์˜ด
        setList(res.data.boxOfficeResult.dailyBoxOfficeList);
      })
      .catch(() => console.error("error!"));

    /**
     * Axios vs Fetch
     * (1) Fetch
     * - ์žฅ์ 
     *    a. js์˜ ๋‚ด์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ! ๊ตณ์ด ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ importํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค
     *    b. ๋‚ด์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—…๋ฐ์ดํŠธ์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
     *
     * - ๋‹จ์ 
     *    a. ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์กด์žฌํ–ˆ์—ˆ์Œ (IE11) >> jQuery + Ajax๊ฐ€ ํฅํ–ˆ๋˜ ์ด์œ !
     *    b. JSON์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค.
     *    c. axios์— ๋น„ํ•ด ๊ธฐ๋Šฅ์ด ์ ๋‹ค.
     *
     * (2) Axios
     * - ์žฅ์ 
     *    a. ๊ธฐ๋Šฅ์ด ๋งŽ๋‹ค.
     *    b. ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ง• ์ตœ์ ํ™” (๋‹ค์–‘ํ•œ ๋ธŒ๋ผ์šฐ์ € ์ง€์›)
     * - ๋‹จ์ 
     *    a. ์„ค์น˜ ํ•„์š”
     */
  }, []);

  return (
    <div className="container">
      <h1>์˜ํ™”์ˆœ์œ„</h1>
      {list.map((item) => (
        <p>
          {item.rank} {item.movieNm} - {item.openDt}
        </p>
      ))}
    </div>
  );
};

export default Ex04;

 

๐ŸŽˆ axios๋กœ ๋‚ ์”จ API ๊ฐ€์ ธ์˜ค๊ธฐ

 

 

import React, { useEffect, useState } from "react";
import axios from "axios";
import "./Ex05.css";

const Ex05 = () => {
  /**
   * https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
   * key : d98ae2c3057b1f588501393dc077fc5f
   */

  let cityList = ["gwangju", "seoul", "busan"];

  const [city, setCity] = useState("gwangju");
  const [temp, setTemp] = useState("0");
  const [icon, setIcon] = useState("");
  const [cloud, setCloud] = useState("");

    const getCity = (e)=>{
        setCity(e.target.innerText)
    }

  const getData = () => {
    console.log("get data");
    let url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=d98ae2c3057b1f588501393dc077fc5f`;
    axios.get(url).then((res) => {
      console.log("res :", res.data.weather[0].icon);

      // ์˜จ๋„ ์„ธํŒ… : ์ผˆ๋นˆ์˜จ๋„ - 273 = ์„ญ์”จ์˜จ๋„
      setTemp((res.data.main.temp - 273).toFixed(1));

      // ๊ตฌ๋ฆ„ ์„ธํŒ…
      if (res.data.clouds.all > 90) {
        setCloud("๋งค์šฐ ํ๋ฆผ");
      } else if (res.data.clouds.all > 60) {
        setCloud("ํ๋ฆผ");
      } else {
        setCloud("๋ง‘์Œ");
      }

      // ์•„์ด์ฝ˜ ์„ธํŒ…
      setIcon(
        `https://openweathermap.org/img/wn/${res.data.weather[0].icon}@2x.png`
      );
    });
  };

  /**์ž„์‹œ ์ฝ”๋“œ
   *
   * Mission!
   * ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ•ด๋‹น ๋„์‹œ๋กœ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋„๋ก ํ•ด๋ณด์ž
   * 1. ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ >> getCity๋ผ๋Š” ํ•จ์ˆ˜๋กœ ์ด๋™ >> city์˜ ์ด๋ฆ„์„ setting
   * 2. city์˜ ์ด๋ฆ„์ด ๋ณ€ํ–ˆ์„ ๋•Œ, city์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๋„๋ก
   * 3. ๋‹จ, ์•„๋ฌด๊ฒƒ๋„ ํด๋ฆญํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ ํ™”๋ฉด์ด ๋œฌ ์ƒํƒœ๋ผ๋ฉด gwangju์˜ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ
   *
   */

  useEffect(() => {
    getData();
  }, [city]);

  return (
    <div className="weather-container">
      <div className="weather-item">
        <h1>์˜ค๋Š˜์˜ ๋‚ ์”จ๐Ÿ’œ</h1>
        <div className="weather-data">
          <img src={icon} width="100px"></img>
          <h1>{temp}โ„ƒ</h1>
          <h3>{city}</h3>
          <h4>{cloud}</h4>
        </div>

        <div className="button-container">
          {cityList.map((item) => (
            <button onClick={getCity} key={item}>{item}</button>
          ))}
        </div>
      </div>
    </div>
  );
};

export default Ex05;