MapleStory Finger Point

๐Ÿ”ต React

[React] Ajax (์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ํ†ต์‹ )

HYEJU01 2024. 8. 31. 01:21

Ajax ?

AJAX ( Asynchronous Javascript and XML) ์€ ์›น ํŽ˜์ด์ง€์˜ ์ด๋™์—†์ด ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์ „์†กํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.

 

  • ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์„œ๋ฒ„์ชฝ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ajax๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ์ด ์ž‘์—…์€ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๋น„๋™๊ธฐ(asynchronous)๋Š” ์š”์ฒญ์ด ๋๋‚  ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋™์‹œ์— ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๋‚˜์ค‘์— react์—์„œ๋Š” ๋‹ค๋ฅธ ์„œ๋ฒ„์˜ REST API์™€ ํ†ต์‹ ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 


ES6 fetch ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ (๋ฆฌ์•กํŠธ)

Promise = fetch(์š”์ฒญ์ฃผ์†Œ)

 

๐Ÿ“ ์ด๋ฒคํŠธ ํด๋ฆญ์‹œ ์ฒ˜๋ฆฌํ•˜๊ธฐ

  • ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ useState()์— ์ €์žฅํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.
  • ๊ทน๋‹จ์ ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ฐ์ดํ„ฐ ํ†ต์‹ ์— 100์ดˆ ๊ฐ€ ์†Œ์š”๋˜๋ฉด useState๋Š” 100์ดˆ ๊ฐ„ undefined ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
  • ๋ Œ๋”๋ง ์‹œ์— ์—๋Ÿฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์—, undefined์— ๊ด€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๋™์‹œ์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
import { useEffect, useState } from "react";

const App = () => {

    let [raw, setRaw] = useState();

    const handleClick = () => {
        fetch('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
        .then( response => response.json() )
        .then( data => {
						//console.log(data)
            setRaw(data)
        })
    }

    return (
        <div>
            <h3>ํด๋ฆญ์‹œ์— fetch๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ</h3>
            <button type="button" onClick={handleClick}>๋ฐ์ดํ„ฐ๋กœ๋“œ</button>
            {raw !== undefined ?
                <div>
                    {raw.userId}<br />
                    {raw.userPw}<br />
                    {raw.userName}<br />
                </div>
                : undefined
            }
        </div>
    )

}

export default App;

 

 

 

 

๐Ÿ“ํ™”๋ฉด ๋ Œ๋”๋ง ์™„๋ฃŒ์‹œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ useEffect() ํ›… ์‚ฌ์šฉ

  • ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ปดํฌ๋„ŒํŠธ์— ๋ฐ”๋กœ ์“ฐ๊ณ  state๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด, ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ useEffect() ํ›…์„ ์ด์šฉํ•˜์—ฌ ์ฒซ๋ฒˆ์งธ ๋ Œ๋”๋ง ์™„๋ฃŒ์‹œ๋งŒ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๋„๋ก ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
import { useEffect, useState } from "react";

const App = () => {

    let [data, setData] = useState()

		//๋ Œ๋”๋ง ์™„๋ฃŒ์‹œ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
    useEffect( () => {
        fetch('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
        .then( response => response.json() )
        .then( data => {
            setData(data)
        })
    }, [])



    return (
        <div>
            <h3>๋กœ๋”ฉ์‹œ์— fetch๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ(๊ฐ™์€ํ‘œํ˜„)</h3>
            {data && <div>
                        {data.userId}<br />
                        {data.userPw}<br />
                        {data.userName}<br />
                    </div>
            }
        </div>
    )

}

export default App;

 

 

 

 


Axios ๋กœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ

์—‘์‹œ์˜ค์Šค๋Š” ๋น„๋™๊ธฐ๋ฅผ ๋”ํŽธํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.

 

 

์—‘์‹œ์˜ค์Šค ์„ค์น˜

npm install axios

 

 

์—‘์‹œ์˜ค์Šค

Promise = axios.get(์š”์ฒญ์ฃผ์†Œ)

 

 

 

๐Ÿ“ ์ด๋ฒคํŠธ ํด๋ฆญ์‹œ ์ฒ˜๋ฆฌํ•˜๊ธฐ (fetch์™€ ๊ฑฐ์˜ ๊ฐ™์Œ ๐Ÿ˜Š)

import axios from "axios";
import { useState } from "react";

const App = () => {

    const handleClick = () => {
        axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
        .then(response => {
            setData( response.data );
        })
    }

    return (
        <div>
            <h3>์—‘์‹œ์˜ค์Šค๋ฐ์ดํ„ฐ</h3>
            <button type="button" onClick={handleClick}>๋ฐ์ดํ„ฐ๋กœ๋“œ</button>
            {data !== undefined ?
                <div>
                    {data.userId}<br />
                    {data.userPw}<br />
                    {data.userName}<br />
                </div>
                :
                undefined
            }
        </div>
    )
}
export default App;

 

 

 


 

async , await ์ ์šฉ

๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ( async(์ด๊ฑฐ ๋น„๋™๊ธฐ์ž„) , await(๊ธฐ๋‹ค๋ ค))

 

๐Ÿ“ async, await๊ทœ์น™

  1. ์–ด์‹ฑํฌ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์–ด์›จ์ž‡์„ ์‚ฌ์šฉํ•œ๋‹ค
  2. function ์•ž์— async ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ ํ•จ์ˆ˜๋Š” ์–ธ์ œ๋‚˜ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ฆฌํ„ด์ด ํ”„๋กœ๋ฏธ์Šค๋ผ๋ฉด await์„ ์ ์šฉํ•˜๊ณ  then์ ˆ์„ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ˜บ๋น„๋™๊ธฐ์—์„œ 3๋ฒˆ์˜ ๋ฐ์ดํ„ฐ ํ†ต์‹ ์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค.

1,2,3,4,5,6์˜ ์ˆœ์„œ๋กœ ์ถœ๋ ฅ๋  ๊ฒƒ ๊ฐ™์ง€๋งŒ ์•„๋‹™๋‹ˆ๋‹ค.

์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

    ...์ƒ๋žต
    const handleClick = () => {
        axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
        .then(response => {
            console.log(response.data);
            console.log(1)
        })
        console.log(2)

        axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hello.json')
        .then(response => {
            console.log(response.data);
            console.log(3)
        })
        console.log(4)

        axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/by.json')
        .then(response => {
            console.log(response.data);
            console.log(5)
        })
        console.log(6)
    }
  •  Axios๋Š” ์ด๋ฏธ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • Axios์•ž์— await์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , then() ์ ˆ์„ ์ƒ๋žต ํ•   ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Axios๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ชจํ•จ์ˆ˜์—๋Š” await์„ ๋ฐ˜๋“œ์‹œ ๋‹ฌ์•„์ค๋‹ˆ๋‹ค.

 

 

๐Ÿ“ async, await์˜ ์žฅ์ 

  1. ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐ์„ฑ
  2. ์–ด์‹ฑํฌ, ์–ด์›จ์ž‡์€ ๋™๊ธฐ์ ๋ฐฉ์‹(์ˆœ์„œ๋ฅผ) ๋ณด์žฅํ•œ๋‹ค

๐Ÿ“  async, await์ ์šฉํ•˜์—ฌ ๋ณ€๊ฒฝํ•˜๊ธฐ

const handleClick = async () => {
    let response = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
    console.log(response.data);
    setData( response.data );

    console.log(1);

    let response2 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hello.json')
    console.log(response2.data);

    console.log(2);

    let response3 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/by.json')
    console.log(response3.data);

    console.log(3);

}

 

 

๐Ÿ“ useEffect์—์„œ async, await์“ฐ๊ธฐ

useEffect(() => {
    
    (async () => {
        let url = '์ฃผ์†Œ';
        let response = await axios.get(url);
				//state์—…๋ฐ์ดํŠธ
    })();


}, []);

 

 


 

import axios from "axios";
import { useEffect } from "react";

function App() {

  const handleClick = async () => {

    // //๋น„๋™๊ธฐ ๋ฐฉ์‹์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ๋ณด์žฅ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด, ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    // axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
    // .then(response => {
    //   console.log(response.data);
    //   console.log(1);

    //   axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hello.json')
    //   .then(response => {
    //     console.log(response.data);
    //     console.log(3);

    //     axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/by.json')
    //     .then(response => {
    //       console.log(response.data);
    //       console.log(5);
    //     });
    //   });
    // });

    //async(์ด๊ฑฐ ๋น„๋™๊ธฐ์•ผ), await(๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๊ธฐ๋‹ค๋ ค)
    //1. asyncํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ awaitํ‚ค์›Œ๋“œ๋ฅผ ์“ธ์ˆ˜ ์žˆ์Œ.
    //2. ๋งŒ์•ฝ function์•ž์— async๋ฅผ ๋ถ™์ด๋ฉด ์–ธ์ œ๋‚˜ Promise๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ
    //3. ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด์ด Promise๋ผ๋ฉด awaitํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๊ณ  then์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Œ

    //์žฅ์ 
    //1. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง
    //2. ์ˆ˜ํ–‰๋ฐฉ์‹์„ ๋™๊ธฐ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊พธ๋ฉด์„œ, ์ˆœ์„œ ๋ณด์žฅ์ด ๋“ค์–ด๊ฐ.
    // const data1 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')

    // console.log(data1.data);
    // console.log(1);
    // const data2 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hello.json')

    // console.log(data2.data);
    // console.log(2);
    // const data3 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/by.json')
    
    // console.log(data3.data);
    // console.log(3);

    // const result = await fetch('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
    //                       .then(response => response.json() )
    // console.log(result);
   }


  //์‹ค์Šต - ํžŒํŠธ๋Š” ์ €์˜ ๊ฐ•์˜์ž๋ฃŒ์— ์žˆ์„๊ฑฐ์—์š” ์•„๋งˆ
  //ํ™”๋ฉด ๋กœ๋“œ์‹œ์— hi.json๋ฐ์ดํ„ฐ๋ฅผ async, await์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

 
  // useEffect(() => {

  //   (async () => {
  //     const result = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json');
  //         console.log(result);
  // }) ();
  // }, []);




  return (
    <>
      <h3>์—‘์‹œ์˜ค์Šค๋กœ ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ</h3>

      <button onClick={handleClick}>ํด๋ฆญ์‹œ ๋„คํŠธ์›Œํฌํ†ต์‹ ์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ</button>

      

    </>
  )

}
export default App;

 

 

 

 

  const handleClick = async () => {

    //๋น„๋™๊ธฐ ๋ฐฉ์‹์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ๋ณด์žฅ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด, ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
    .then(response => {
      console.log(response.data);
      console.log(1);

      axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hello.json')
      .then(response => {
        console.log(response.data);
        console.log(3);

        axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/by.json')
        .then(response => {
          console.log(response.data);
          console.log(5);
        });
      });
    });

....

 

 

  //async(์ด๊ฑฐ ๋น„๋™๊ธฐ์•ผ), await(๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๊ธฐ๋‹ค๋ ค)
    //1. asyncํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ awaitํ‚ค์›Œ๋“œ๋ฅผ ์“ธ์ˆ˜ ์žˆ์Œ.
    //2. ๋งŒ์•ฝ function์•ž์— async๋ฅผ ๋ถ™์ด๋ฉด ์–ธ์ œ๋‚˜ Promise๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ
    //3. ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด์ด Promise๋ผ๋ฉด awaitํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๊ณ  then์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Œ

    //์žฅ์ 
    //1. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง
    //2. ์ˆ˜ํ–‰๋ฐฉ์‹์„ ๋™๊ธฐ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊พธ๋ฉด์„œ, ์ˆœ์„œ ๋ณด์žฅ์ด ๋“ค์–ด๊ฐ.
    const data1 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')

    console.log(data1.data);
    console.log(1);
    const data2 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hello.json')

    console.log(data2.data);
    console.log(2);
    const data3 = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/by.json')
    
    console.log(data3.data);
    console.log(3);

    const result = await fetch('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json')
                          .then(response => response.json() )
    console.log(result);

 

 

  useEffect(() => {

    (async () => {
      const result = await axios.get('https://raw.githubusercontent.com/yopy0817/data_example/master/hi.json');
          console.log(result);
  }) ();
  }, []);