[TIL]21.08.17

๋ฐ•์ฃผํ™ยท2021๋…„ 8์›” 17์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
51/104

๐Ÿ‘จโ€๐Ÿ’ป์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

  • ์˜ค๋Š˜์€ useEffect์— ๋Œ€ํ•˜์—ฌ ๊ถ๊ธˆํ–ˆ๋˜ ๊ฒƒ๋“ค์„ ์ •๋ฆฌํ•˜์—ฌ ํ•™์›(์ฝ”๋“œ์Šคํ…Œ์ด์ธ )์— ์งˆ๋ฌธํ–ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ํ’€๋ฆฌ์ง€์•Š์•„ ํž˜๋“ค์—ˆ์œผ๋‚˜ ์งˆ๋ฌธ์„ ์ •๋ฆฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๋‚ด๊ฐ€ ๋ฌด์—‡์„ ๋ชจ๋ฅด๊ณ  ์•„๋Š”์ง€์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ์ •๋ฆฌ๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™์•„ ๋‹คํ–‰์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค

  • ํ˜„์žฌ ์–ด๋–ค ์Šคํ”„๋ฆฐํŠธ๋ฅผ ์ง„ํ–‰ ์ค‘์ด๊ณ , ์–ด๋–ค ๋ฌธ์ œ์— ๋ถ€๋”ชํ˜”๋‚˜์š”?
    HA์‹œํ—˜ ์œ„ํ•ด useEffect๋ฅผ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋˜ ์ค‘ cleanupํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•„์„œ ์งˆ๋ฌธ ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

    ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋Š” Ajax์š”์ฒญ์„ ํ•ด์„œ state๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  value๋ž€ state๋Š” ๊ฐ ์ปดํฌ๋„ŒํŠธ์— ๊ฐ€์„œ ๋ Œ๋”๋ง๋˜๊ฒŒ ๋˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

    const [value, setValue] = useState([]);
    useEffect(()=>{
     async () => {
      let URL = 'GET์š”์ฒญํ•  ์ฃผ์†Œ..'
      fetch(URL)
      .then((res) => res.json())
      .then((el) => {
       setValue(el);
      })
     }
    }, [])

    ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด

    Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

    ์™€ ๊ฐ™์ด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋˜์„œ cleanup ํ•จ์ˆ˜๋ฅผ ์จ์•ผํ•œ๋‹ค. ๋ผ๊ณ  ํ•ด์„œ Ajax์š”์ฒญ์„ useEffect์—์„œ ํ•œ๋ฒˆ๋งŒ ๋˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋‹ˆ ์–ด์ฐŒ์–ด์ฐŒ ์œ„์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๋Š” ๋‚˜์ง€์•Š๋Š”๋ฐ, ์ดํ•ด๊ฐ€ ๋˜์งˆ ์•Š์Šต๋‹ˆ๋‹ค..

    ์• ์ดˆ์— useEffect์˜ ๋‘๋ฒˆ์งธ์ธ์ž์— ๋นˆ๋ฐฐ์—ด ํ˜น์€ ์ธ์ž๋ฅผ ๋„ฃ์ง€ ์•Š์œผ๋ฉด ๋ฆฌ์•กํŠธ๊ฐ€ ์ฒ˜์Œ ๋ Œ๋”๋ง์ด ๋œ ์ดํ›„์— useEffect๊ฐ€ ํ•œ๋ฒˆ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ๋Š”๋ฐ ์ œ๊ฐ€ ์•„๋Š” ๋Œ€๋กœ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๋ฉด ์ €๋ ‡๊ฒŒ ์™œ cleanupํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์„œ Ajax์š”์ฒญ์ด ํ•œ๋ฒˆ๋งŒ ๋˜๋„๋กํ•ด์•ผํ•˜๋Š”์ง€, ์™œ ์ €๋ ‡๊ฒŒ cleanup ํ•จ์ˆ˜๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์„ useEffect์—์„œ ๊ตฌํ˜„ํ•ด์•ผ ๋˜๋Š”์ง€ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

    const [value, setValue] = useState([]);
    useEffect(()=>{
     let forCleanup = true; 
     async () => {
      let URL = 'GET์š”์ฒญํ•  ์ฃผ์†Œ..'
      fetch(URL)
      .then((res) => res.json())
      .then((el) => {
       if(forCleanup){
       setValue(el);
      }
      })
     }
     return ()=>{ 
       forCleanup = false;
     }
    }, [])

    ์•ˆ ๋˜๋Š” ๋ถ€๋ถ„์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ค ๋…ธ๋ ฅ์„ ํ–ˆ๋‚˜์š”?
    ๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ์™€ ์—ฌ๋ ค ๊ธ€๋“ค์„ ์ฐธ์กฐํ•˜์—ฌ ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๊ณ ์ณ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
    ๋˜ํ•œ useEffect์˜ ๋‘๋ฒˆ์งธ์ธ์ž๊ฐ€ ๋นˆ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ ์ฒ˜์Œ ๋ Œ๋”๋งํ›„ ํ•œ๋ฒˆ๋งŒ useEffect๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์•Œ๊ณ ์žˆ์–ด์„œ AJAX์š”์ฒญ ๋˜ํ•œ ํ•œ๋ฒˆ๋งŒ ๋˜๋Š”์ง€ ์•Œ๊ณ ์žˆ์—ˆ๋Š”๋ฐ, cleanup ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์™€์„œ AJAX์š”์ฒญ์ด ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๊ฒŒ ์œ„์™€๊ฐ™์ด ํ–ˆ๋”๋‹ˆ ์˜ค๋ฅ˜๋Š” ์‚ฌ๋ผ์ง€๋Š”๋ฐ, ์™œ ์ €๋ ‡๊ฒŒ cleanupํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ๋ฉด ๋งค๋ชจ๋ฆฌ๋ˆ„์ˆ˜๊ฐ€ ๋œ๋‹ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋Š”์ง€๊นŒ์ง€ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๊ธฐ๊นŒ์ง€ ์—ฌ๋Ÿฌ ๊ฒ€์ƒ‰๊ณผ ์‹œ๋„๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค...

    profile
    ๊ณ ํ†ต์—†๋Š” ์„ฑ์žฅ์€ ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค....

    0๊ฐœ์˜ ๋Œ“๊ธ€