Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

React useState(), useEffect() ๋ณธ๋ฌธ

React

React useState(), useEffect()

kohi โ˜• 2022. 11. 15. 14:16

 

๐Ÿšฉ useState()


  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ ์ธ ๊ฐ’์„ ์ƒํƒœ(state)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜์„ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ’์ด ๋™์ ์œผ๋กœ ๋ฐ”๋€” ๊ฒฝ์šฐ์—๋Š” ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.
  • Hooks ๊ธฐ๋Šฅ์ด ๋„์ž…๋œ ๋ฆฌ์•กํŠธ 16.8 ๋ฒ„์ „๋ถ€ํ„ฐ useState() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿšฉ useState() ์‚ฌ์šฉ ํ๋ฆ„


useState()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ์˜ ์„ธ ๋‹จ๊ณ„๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

import { useState } from "react"; // โ“

  function App() {
    const [count, setCount] = useState(0); // โ“‘
    return (
      <div>
        <p>You clicked {count} times</p>
        <button onClick={() => setCount(count + 1)}></button> // โ“’
      </div>
    );
  }

โ“ useState()๋ฅผ React์—์„œ import

โ“‘ useState()๋กœ state ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ์—ฌ๊ธฐ์„œ ๋Œ€๊ด„ํ˜ธ๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ '๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด' ๋ผ๋Š” JavaScript ๋ฌธ๋ฒ•์ด๋‹ค.

โ“’ useState()๊ฐ€ ๋งŒ๋“ค์–ด ์ค€ state๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋กœ state ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค. React๋Š” ๋ณ€ํ™”๋ฅผ ์ธ์ง€ํ•˜๊ณ  ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌ๋ Œ๋”๋ง ํ•œ๋‹ค.

 

๐Ÿšฉ useState() ์„ธ๋ถ€ ์‚ฌํ•ญ


const [showing, setShowing] = useState(false);
  • useState์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ false๋Š” showing์˜ state ์ดˆ๊ธฐ๊ฐ’์ด๊ณ , useState๋Š” false๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” showing state์™€ showing์˜ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” setShowing ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

const onClick = () => {
        setShowing((prev) => !prev);
    };
  • ํด๋ฆญ ์‹œ state ๊ฐ’ ๋ณ€๊ฒฝ

 

return (
        <div>
            <button onClick={onClick}>{showing ? "Hide" : "Show"}</button>
        </div>
    );
  • state ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
  • state๊ฐ€ false(์ดˆ๊ธฐ๊ฐ’)์ธ ๊ฒฝ์šฐ ๋ฒ„ํŠผ ๋‚ด ํ…์ŠคํŠธ๋กœ Show ์ถœ๋ ฅ, true์ธ ๊ฒฝ์šฐ Hide ์ถœ๋ ฅ

 

๐Ÿšฉ useEffect()


  • useEffect()๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋  ๋•Œ๋งˆ๋‹ค ํŠน์ • ์ž‘์—…์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” Hook์ด๋‹ค. useEffect()๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ mount ๋์„ ๋•Œ, unmount ๋์„ ๋•Œ, update ๋์„ ๋•Œ ํŠน์ • ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋งˆ์šดํŠธ(mount): ๋งˆ์šดํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ตœ์ดˆ๋กœ ๋ Œ๋” ๋  ๋•Œ ๊ฑฐ์น˜๋Š” ๊ณผ์ •์ด๋‹ค. ์ดํ›„ props์™€ state๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋Š” ๋งˆ์šดํŠธ ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š๋Š”๋‹ค.
    • ์–ธ๋งˆ์šดํŠธ(unmount): ์–ธ๋งˆ์šดํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ญ์ œ๋  ๋•Œ ๊ฑฐ์น˜๋Š” ๊ณผ์ •์ด๋‹ค.

 

useEffect() ์‚ฌ์šฉ ์˜ˆ์ œ
function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setTimeout(() => {
      console.log(`You clicked ${count} times`);
    }, 3000);
  });
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

 

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ
componentDidUpdate() {
  setTimeout(() => {
     console.log(`You clicked ${this.state.count} times`);
  }, 3000);
}

 

๐Ÿšฉ useEffect() ์„ธ๋ถ€ ์‚ฌํ•ญ


์ปดํฌ๋„ŒํŠธ์˜ ๋žœ๋”๋ง ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๋Š” (์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ, ์ดํŽ™ํŠธ, ํƒ€์ž„์•„์›ƒ์ด๋‚˜ ๊ทธ ์•ˆ์—์„œ ํ˜ธ์ถœ๋˜๋Š” API ๋“ฑ) ๋žœ๋”(render)๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ •์˜๋œ props์™€ state ๊ฐ’์„ ์žก์•„๋‘”๋‹ค.

useEffect(callback[, dependencies]);
  • useEffect()๋Š” 2๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š”๋‹ค.
    • callback => ์‹คํ–‰ํ•  ํ•จ์ˆ˜, dependencyArray => dependency array๋ฅผ ํ†ตํ•ด useEffect()๋Š” ์กฐ๊ฑด๋ถ€๋กœ callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. (dependency array ๋ณ€๊ฒฝ ์‹œ) ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋งค๋ฒˆ ๋ Œ๋”๋ง ์ดํ›„์— callback ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

 

useEffect(() => {
  document.title = `Greetings to ${name}`;
}, [name]);
  • ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋ Œ๋”๋ง๋  ๋•Œ๋งˆ๋‹ค title์ด ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, [name] ์†์„ฑ์ด ๋ณ€๊ฒฝ๋  ๋•Œ callback์ด ์‹คํ–‰๋œ๋‹ค.

 

useEffect(() => {
  document.title = `Greetings to ${name}`;
});
  • dependency array๋ฅผ ๋„˜๊ธฐ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ๋งค ๋ Œ๋” ์‹œ๋งˆ๋‹ค ์‹คํ–‰

 

useEffect(() => {
  document.title = `Greetings to ${name}`;
}, []);
  • useEffect์— ๋นˆ ๋ฐฐ์—ด์„ ๋„ฃ๋Š” ๊ฒฝ์šฐ๋Š” ์ฒ˜์Œ์œผ๋กœ ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ, ์ฆ‰ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ตœ์ดˆ๋กœ ๋ Œ๋”๋ง ๋˜๋Š” ๋งˆ์šดํŠธ (mount)์‹œ์— ๋‹จ ํ•œ๋ฒˆ ์‹คํ–‰๋  ๋•Œ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

useEffect(() => {
        console.log("hi :)");
        return () => console.log("bye :(");
    }, []);

  • ์–ธ๋งˆ์šดํŠธ๋Š” ๋นˆ ๋ฐฐ์—ด๊ณผ ํด๋ฆฐ์—… ํ•จ์ˆ˜๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒ๊ธธ ๋•Œ๋Š” ์ฝ˜์†” ์ฐฝ์— hi :) ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—†์–ด์ง€๋Š” ์ˆœ๊ฐ„์—๋Š” ์ฝ˜์†” ์ฐฝ์— bye :( ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

๐Ÿšฉ Reference


https://dmitripavlutin.com/react-useeffect-explanation/

https://nomadcoders.co/react-for-beginners/lobby

https://iforint.tistory.com/157

https://jongminfire.dev/react-use-effect-ํ›…-์ดํŽ™ํŠธ-ํ•จ์ˆ˜-ํด๋ฆฐ์—…-ํ•จ์ˆ˜

https://overreacted.io/ko/a-complete-guide-to-useeffect/

https://sgwanlee.medium.com/useeffect์˜-dependency-array-ebd15f35403a

https://xiubindev.tistory.com/97

https://xiubindev.tistory.com/100

https://daesiker.tistory.com/5