WEB/REACT

[โ˜„๏ธํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…] useEffect ๋‚ด setState ๋ฌดํ•œ๋ฃจํ”„

mingzoo 2023. 10. 5. 17:14

์›๋ž˜ ๋ฌธ์ œ๊ฐ€ ์—†๋˜ ์ฝ”๋“œ์— ์ด๋ฒˆ์— border๋ฅผ linear๋กœ ๋ฐ”๊พธ๋ฉด์„œ setํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋‘๊ฐœ ๋„ฃ์–ด์ฃผ๋‹ค๋ณด๋‹ˆ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

# ์›๋ž˜ ์ฝ”๋“œ
useEffect(() => {
    if (isPost.data?.isAllSeen == false) {
      for (let i = 0; i < isPost.data?.seenPostList.length; i++) {
        if (isPost.data?.seenPostList[i].isSeen == false) {
          if (isPost.data?.seenPostList[i].isPublic == true) {
            setBorderColor(['#6935FD', '#9747FF']);
            setIndex(i);
            break;
          }
          if (isPost.data?.seenPostList[i].isPublic == false) {
            setBorderColor(['#FFAC7E', '#FFE4BC']);
            setIndex(i);
            break;
          }
        }
      }
    }
  }, [isPost]);

์œ„์— ์ฒ˜๋Ÿผ ๊ณต๊ฐœํฌ์ŠคํŠธ์ผ ๊ฒฝ์šฐ์—๋Š” ์œ„์— if๋ฌธ์ด ๋Œ๊ฒŒ ๋˜๊ณ , ๊ทธ๊ฒŒ ์•„๋‹ ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜ if๋ฌธ์„ ๋Œ๋ฉด์„œ setํ•จ์ˆ˜๊ฐ€ ๋Œ๊ฒŒ๋” ๋กœ์ง์„ ์งฐ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์•„๋ž˜ warning์ด ๋–ด๋‹ค.

Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.

์ด ๋ง์€ useEffect ์•ˆ์— setState๋ฅผ ๋Œ๋ฆฌ๊ฒŒ ๋˜๋ฉด ๋ฌดํ•œ๋ฃจํ”„๊ฐ€ ๋Œ๊ฒŒ ๋œ๋‹ค๋Š” ๊ฑฐ๋‹ค.

์™œ ๊ทธ๋Ÿด๊นŒ?

useEffect ๋‚ด์—์„œ setState๋ฅผ ํ˜ธ์ถœ๋˜๋ฉด -> ์ปดํฌ๋„ŒํŠธ ์žฌ๋ Œ๋”๋ง -> useEffect ๋˜ ํ˜ธ์ถœ -> ๋˜ setState -> ๋˜ ๋ Œ๋”๋ง..

์ด๋ ‡๊ฒŒ ๋ฌดํ•œ ๋ฃจํ”„์˜ ๋Šช์— ๋น ์ง€๊ฒŒ ๋œ๋‹ค.

 

๋‘๊ฐ€์ง€ ํ•ด๊ฒฐ๋ฒ•์„ ์ ์šฉํ–ˆ๋‹ค.

1. ์กฐ๊ฑด ๊ฒ€ํ† 

2. ์˜์กด์„ฑ ๋ฐฐ์—ด ๊ฒ€ํ† 

 

์ผ๋‹จ ์กฐ๊ฑด์„ ๊ฒ€ํ† ํ•ด๋ดค๋‹ค.

์•„์ง ๋ณด์ง€ ์•Š์€ ํฌ์ŠคํŠธ์— ์žˆ์–ด์„œ ์ƒ‰์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๋กœ์ง์„ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ˆ˜์ •ํ–ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ๋„ ๊ณ„์†ํ•ด์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ธธ๋ž˜ ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ๊ณ  ํ•˜๊ณ  ๋“ค์—ฌ๋‹ค๋ดค๋‹ค.

์•„์ง useEffect๊ฐ€ 3๋ฒˆ ์ด์ƒ์ด ์‹คํ–‰๋˜๋Š” ๊ฑธ ๋ฐœ๊ฒฌํ–ˆ๊ณ , 2๋ฒˆ ํ•ด๊ฒฐ๋ฒ•์ด์—ˆ๋˜ ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ๊ฒ€ํ† ํ–ˆ๋‹ค.

์›๋ž˜๋Š” isPost๋ฅผ ์˜์กด์„ฑ ๋ฐฐ์—ด ์•ˆ์— ๋„ฃ์–ด๋†จ์—ˆ๋Š”๋ฐ, ๋„ˆ๋ฌด ๋งŽ์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ๋ฒ”์œ„๋ฅผ ์ข€ ๋” ์ขํ˜”๋‹ค.

๋ณธ ํฌ์ŠคํŠธ ๋ชฉ๋ก์ด ์ˆ˜์ •๋๋‹ค๋Š” ๊ฑด => ํฌ์ŠคํŠธ๋ฅผ ์ตœ์†Œ ํ•˜๋‚˜๋ผ๋„ ์ฝ์—ˆ๋‹ค๋Š” ๋œป์ด๊ณ , ๊ทธ๋•Œ ์‹คํ–‰์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค

useEffect(() => {
    if (isPost.data?.isAllSeen === false) {
      const unseenPostIndex = isPost.data?.seenPostList.findIndex(post => !post.isSeen);

        const color = isPost.data?.seenPostList[unseenPostIndex].isPublic
          ? ['#6935FD', '#9747FF']
          : ['#FFAC7E', '#FFE4BC'];
      
        setBorderColor(color);
        setIndex(unseenPostIndex);
    }
    else {
      setBorderColor(['#101011','#101011'])
    }
  }, [isPost.data?.seenPostList]);

์ด๋ ‡๊ฒŒ ๋‘๊ฐ€์ง€ ํ•ด๊ฒฐ๋ฒ•์„ ๋ชจ๋‘ ์ ์šฉํ–ˆ๋”๋‹ˆ ๋ฌธ์ œ์—†์ด ์ž˜ ๋Œ์•„๊ฐ”๋‹ค!!

 

์ด๋ฒˆ ๊ธฐํšŒ๋ฅผ ํ†ตํ•ด useEffect ์•ˆ์— setํ•จ์ˆ˜๊ฐ€ ๋Œ์•„๊ฐ€๋ฉด์„œ ๋ฌดํ•œ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š” ์ด์œ ๋ž‘ ๊ทธ์— ๋งž๋Š” ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๊ณ , useEffect๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ด ๋ถ€๋ถ„์„ ์กฐ์‹ฌํ•ด์„œ ๋กœ์ง์„ ์งœ์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

728x90