useState()

1. ๊ฐœ์š”

๋ฆฌ์•กํŠธ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ›…์„ ํ•˜๋‚˜ ๋งํ•˜์ž๋ฉด useState()๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. useState()๋Š” ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด state๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ›…์ด๋‹ค. ์ฆ‰, useState()๋ฅผ ํ†ตํ•ด ๋™์ ์œผ๋กœ ๋ณ€ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.


2. useState()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค?!

import React, { useState } from "react";

const Counter = () => {
  let number = 1;

  const onIncrease = () => {
    number += 1;
    console.log("์ฆ๊ฐ€ / number:", number);
  };

  const onDecrease = () => {
    number -= 1;
    console.log("๊ฐ์†Œ / number:", number);
  };

  return (
    <div>
      <h1>์นด์šดํŠธ: {number}</h1>
      <button onClick={onIncrease}>+</button>
      <button onClick={onDecrease}>-</button>
    </div>
  );
};

export default Counter;

์œ„์˜ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์นด์šดํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๊ฑฐ๋‚˜ ๊ฐ์†Œ์‹œํ‚ค๊ณ  ํ˜„์žฌ์˜ ์นด์šดํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋‹จ์ˆœํ•œ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

ํ•˜์ง€๋งŒ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€, ๊ฐ์†Œ๋ฅผ ์‹œํ‚ค๋ฉด ์นด์šดํŠธ์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ณ€์ˆ˜ number๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š์•„์„œ ์ผ๊นŒ? ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜์— ์ฝ˜์†”๋กœ number๊ฐ’์„ ์ฐ์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณ„์† ๋ณ€ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

number๊ฐ’์ด ๊ณ„์†ํ•ด์„œ ๋ณ€ํ•˜์ง€๋งŒ ํ™”๋ฉด์— ๋ณ€ํ™˜ number๊ฐ’์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋ฆฌ์•กํŠธ๊ฐ€ ์žฌ๋žœ๋”๋ง์„ ํ•˜์ง€ ์•Š์•„์„œ ์ด๋‹ค.

number์˜ ๊ฐ’์ด ๋™์ ์œผ๋กœ ๊ณ„์†ํ•ด์„œ ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” useState()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ…Œ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.


3. useState() ์‚ฌ์šฉํ•˜๊ธฐ

useState() ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

const [๋ณ€์ˆ˜๋ช…, setํ•จ์ˆ˜๋ช…] = useState(์ดˆ๊นƒ๊ฐ’); ๋ณดํ†ต setํ•จ์ˆ˜ํ˜์€ ๋ณ€์ˆ˜๋ช… ์•ž์— set๋ฅผ ๋ถ™์ธ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ๋ณ€์ˆ˜๋ฅผ name์ด๋ผ๊ณ  ์ง€์—ˆ์œผ๋ฉด setํ•จ์ˆ˜๋ช…์€ setName์œผ๋กœ ์ง–๋Š”๋‹ค. ์ดˆ๊นƒ๊ฐ’์€ ์–ด๋–ค ํƒ€์ž…์œผ๋กœ ์ •ํ•ด๋„ ๋˜๊ณ  ์ƒ๋žต์„ ํ•ด๋„ ๋œ๋‹ค. ์ƒ๋žต์„ ํ–ˆ์„ ๋•Œ๋Š” undefined๊ฐ€ ๋œ๋‹ค.

useState() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์˜ Counter ์ปดํฌ๋„ŒํŠธ์˜ number๊ฐ’์„ ๊ด€๋ฆฌํ•ด๋ณด์ž.

import React, { useState } from "react";

const Counter = () => {
  const [number, setNumber] = useState(1);

  const onIncrease = () => {
    setNumber((prev) => prev + 1);
    console.log("์ฆ๊ฐ€ / number:", number);
  };

  const onDecrease = () => {
    setNumber((prev) => prev - 1);
    console.log("๊ฐ์†Œ / number:", number);
  };

  return (
    <div>
      <h1>์นด์šดํŠธ: {number}</h1>
      <button onClick={onIncrease}>+</button>
      <button onClick={onDecrease}>-</button>
    </div>
  );
};

export default Counter;

์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด +, - ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ ๋งˆ๋‹ค number๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

์ฝ˜์†”์ฐฝ๋„ ํ•จ๊ป˜ ๋ณด๋ฉฐ ๋น„๊ตํ•ด๋ณด์ž.

ํ™”๋ฉด์—๋Š” number๊ฐ’์ด 4๋ผ๊ณ  ๋‚˜์˜ค์ง€๋งŒ ์ฝ˜์†”์—๋Š” ์ฆ๊ฐ€ / number: 3์ด ์ฐํžˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” inCrease()ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ฝ˜์†”์ด ์ฐํžŒ ํ›„ number๊ฐ’์ด ๋ฐ”๋€Œ๊ณ  ๋‚˜์„œ ์žฌ๋ Œ๋”๋ง์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

  1. setNumber((prev) => prev + 1); ํ•จ์ˆ˜ ์‹คํ–‰ (๋น„๋™๊ธฐ)

  2. console.log("์ฆ๊ฐ€ / number:", number); ํ•จ์ˆ˜ ์‹คํ–‰

  3. number๊ฐ’์ด ๋ฐ”๋€Œ๋ฉฐ ํŽ˜์ด์ง€๊ฐ€ ์žฌ๋ Œ๋”๋ง

  4. <h1>์นด์šดํŠธ: {number}</h1>์— ๋ณ€ํ•œ๋œ number๊ฐ’์ด ์ ์šฉ๋œ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด! inCrease()ํ•จ์ˆ˜๋‚ด์˜ consolo.log์— ๋ณ€๊ฒฝ๋œ number๊ฐ’์ด ์ฐํžˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ์ฆ‰, ์žฌ๋ Œ๋”๋ง์ด ๋˜๋ฉด์„œ ๋™์‹œ์— ๋ณ€ํ™”๋œ state๊ฐ’(์—ฌ๊ธฐ์„œ๋Š” number)์„ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šธ useEffect()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ useEffect()์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃฌ๋‹ค.


4. setState()

setState()๋Š” ๋ฆฌ์•กํŠธ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๋‚ด์„œ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” hooks์ธ useState()๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. setState()๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•์ด ์žˆ๋‹ค.

  1. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

  2. state ๊ฐ์ฒด๋ฅผ ๋„˜๊ฑฐ์ค„ ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด state๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.

setState()์˜ ์ „๋‹ฌ์ธ์ž์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž

์•„๋ž˜๋Š” setState()์˜ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ ์ฝ”๋“œ์ด๋‹ค.

type SetStateAction<S> = S | ((prevState: S) => S);

์œ„์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜์ž๋ฉด setState()์˜ ์ „๋‹ฌ์ธ์ž๋Š” ๋‘ ์ข…๋ฅ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

  1. S: state๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝ๋˜์–ด์ ธ์•ผ ํ•œ๋‹ค๋ผ๋Š” ๊ทธ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

  2. ((prevState: S) => S): ์ด์ „์˜ state๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ  ์ƒˆ๋กœ์šด state๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜(์ฝœ๋ฐฑํ•จ์ˆ˜)๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

3. useState ์‚ฌ์šฉํ•˜๊ธฐ์—์„œ๋Š” ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์ž๋ฅผ ๋„ฃ์—ˆ๋‹ค.


5. Conclusion

useState()๋Š” ๋‹ค์Œ์— ๋ฐฐ์šธ useEffect()์™€ ํ•จ๊ป˜ ๋ฆฌ์•กํŠธ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํ›…์ด๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š” ๋‚ด์šฉ์ด๋ผ ๊ณต๋ถ€ํ•˜๊ณ  ์ดํ•ดํ•˜๋Š” ๋ฐ์—๋Š” ํฐ ์–ด๋ ค์›€์„ ๋Š๋ผ์ง„ ์•Š์•˜๋‹ค. ํ•˜์ง€๋งŒ setState()ํ•จ์ˆ˜๊ฐ€ ๋น„๋™๊ธฐ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ ์ „์—๋Š” ๊ทธ๋ƒฅ state๊ฐ’์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ผ๋Š” ์ •๋„๋งŒ ์•Œ๊ณ  ์žˆ์—ˆ๋‹ค. ํ•ด๋‹น ๋‚ด์šฉ์— ๋Œ€ํ•œ ํ•™์Šต์ด ์žˆ๊ธฐ ์ „์—๋Š” "์™œ setState()๋ฅผ ์‚ฌ์šฉํ•ด์„œ state๊ฐ’์„ ๋ฐ”๊พธ์—ˆ๋Š”๋ฐ ๋ฐ”๋กœ ์ ์šฉ์ด ๋˜์ง€ ์•Š์„๊นŒ?"๋ผ๋Š” ๊ถ๊ธˆ์ฆ์ด ์žˆ์—ˆ๋Š”๋ฐ ๊ถ๊ธˆ์ฆ์ด ํ•ด๊ฒฐ๋˜์–ด์„œ ์†์ด ๋ปฅ ๋šซ๋ฆฐ ๊ธฐ๋ถ„์ด๋‹ค.๐Ÿ˜ƒ


์ฐธ๊ณ 

๋„์„œ - ์†Œํ”Œ์˜ ์ฒ˜์Œ ๋งŒ๋‚œ ๋ฆฌ์•กํŠธ 7. useState ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ”๋€Œ๋Š” ๊ฐ’ ๊ด€๋ฆฌํ•˜๊ธฐ React์˜ setState() ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ


๐Ÿ‘†

๐Ÿ“… 2022-07-26

Last updated