[React] ๐Ÿ“š State

pyeonneยท2022๋…„ 6์›” 13์ผ
0
post-thumbnail

๐Ÿ“‹ ๊ธฐ์–ตํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ

๐Ÿ“Œ useState() hook

  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” JSX๋ฅผ return ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค.
  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์ผ์ด ํ•„์š”ํ•˜๋‹ค.
  • ์ด๋ฅผ ์œ„ํ•ด์„œ React์—์„œ๋Š” useState() hook์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
import React, { useState } from "react";

const [state, setSate] = useState();
  • setState()๋Š” ์ปดํฌ๋„ŒํŠธ์˜ re-rendering์„ ๋ฐœ์ƒ์‹œ์ผœ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ค€๋‹ค.

๐Ÿ“Œ ๋ถˆ๋ณ€์„ฑ (immutable)

  • ๋ถˆ๋ณ€์„ฑ์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์„ฑ์งˆ์„ ์ผ์ปซ๋Š”๋‹ค.

  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚จ๋‹ค๋Š” ๋ง์€ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์˜ ๊ฐ’์„ ์ง์ ‘์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

  • setState() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋ฌด์กฐ๊ฑด re-rendering์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

    • setState() ํ•จ์ˆ˜ ์ด์ „์˜ state ๊ฐ’๊ณผ setState() ํ•จ์ˆ˜ ์ดํ›„์˜ state ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๋งŒ์•ฝ ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด re-rendring์ด ์ผ์–ด๋‚œ๋‹ค.
  • ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚ค์ง€ ์•Š๊ณ , ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์˜ ๊ฐ’์„ ์ง์ ‘ ๋ณ€๊ฒฝํ•˜๋ฉด React๋Š” state๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋‹ค๊ณ  ์ธ์ง€ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

    • ์™œ๋ƒํ•˜๋ฉด React๋Š” ์ด์ „ state ๊ฐ’๊ณผ ์ดํ›„ state ๊ฐ’์„ ๋น„๊ตํ•  ๋•Œ, ์–•์€ ๋น„๊ต(Shallow Compare)๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

    ์–•์€ ๋น„๊ต์—์„œ ๋‘ ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฐ™๋‹ค๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.

์›์‹œ ํƒ€์ž… VS ์ฐธ์กฐ ํƒ€์ž…

  • ์›์‹œ ํƒ€์ž…์€ ๋ถˆ๋ณ€์„ฑ์„ ์ด๋ฏธ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— setState()๋ฅผ ํ•  ๋•Œ ๋”ฑํžˆ ๊ณ ๋ คํ•  ์ ์ด ์—†์ง€๋งŒ, ์ฐธ์กฐ ํƒ€์ž…์€ ๋ถˆ๋ณ€์„ฑ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— setSate()๋ฅผ ํ•  ๋•Œ ๋ถˆ๋ณ€์„ฑ์„ ๊ฐ€์ง€๋„๋ก ํ˜ธ์ถœํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ฐธ์กฐ ํƒ€์ž… state ๋‹ค๋ฃจ๊ธฐ

const Counter = () => {
  const operators = ["+", "-", "*"];
  const [info, setInfo] = useState({
    count: 0,
    show: true,
    operator: operators[0],
  });

  return (
    <div>
      <button onClick={() => setInfo({ ...info, show: !info.show })}>
        Show and Hide
      </button> 
    </div>
  );
};
  • Array๋„ ์ฐธ์กฐ ํƒ€์ž…์ด๋‹ค.

๐Ÿ“Œ props์™€ state

props

  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ๊ฐ’์ด๋‹ค.
  • ๊ฐ’์„ ์ž์‹  ํ˜น์€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

state

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์Šค์Šค๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ ๊ฐ’์ด๋‹ค.
  • setState() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

props๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ๋‚ด๋ ค ๋ฐ›๊ฑฐ๋‚˜ ์ž์‹ ์ด ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•œ๋‹ค.


๐Ÿ“Œ State ์‹ฌํ™”

๋น„๋™๊ธฐ๋กœ ์ด๋ฃจ์–ด์ง€๋Š” setState()

  • setState() ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.
  • ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ”ผํ• ๋ ค๋ฉด setState() ์ธ์ž๋ฅผ ํ•จ์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.
const [number, setNumber] = useState(1);

const add = () => setNumber((number) => number + 1);
const subtract = () => setNumber((number) => number - 1);
const multiplyBy2 = () => setNumber((number) => number * 2);

๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„

  • React์˜ ๋ฐ์ดํ„ฐ๋Š” ํ•ญ์ƒ ์•„๋ž˜๋กœ ํ๋ฅธ๋‹ค.
  • state๋Š” ํ•ญ์ƒ ํŠน์ •ํ•œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ๊ทธ state๋กœ๋ถ€ํ„ฐ ํŒŒ์ƒ๋œ UI ๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ์˜ค์ง ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๋งŒ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

state ๋Œ์–ด์˜ฌ๋ฆฌ๊ธฐ

  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์š”์†Œ๋ฅผ ํ•œ ๊ณณ์—์„œ๋งŒ ์ œ์–ด ๋˜๋Š” ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹ค.
  • ๋™์ผํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์— ๋ฐ˜์˜ํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์ข…์ข… ์žˆ๋Š”๋ฐ, ์ด๋Ÿด ๋•Œ๋Š” ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ state๋ฅผ ๋Œ์–ด์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๐ŸŒ ์ˆ˜๊ฐ•ํ•œ ์†Œ๊ฐ & ๋– ์˜ค๋ฅด๋Š” ์ƒ๊ฐ

  • state ๋Œ์–ด์˜ฌ๋ฆฌ๊ธฐ ์‹ค์Šต์„ ํ•˜๋ฉด์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๊ณณ์—์„œ๋งŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ฌ์•˜๋‹ค. state๋ฅผ ๋Œ์–ด์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด ์•„์ง ๋‚ฏ์„ค๊ณ  ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ํ—ท๊ฐˆ๋ฆฌ์ง€๋งŒ, ๋ฐ˜๋ณตํ•ด์„œ ์—ฐ์Šตํ•˜๋„๋ก ํ•˜์ž!

๐Ÿ”— ์ฐธ๊ณ  ๋ž˜ํผ๋Ÿฐ์Šค

profile
๊ฑด๊ฐ•ํ•œ ๋ชธ๊ณผ ๋งˆ์Œ์—์„œ ๊ฑด๊ฐ•ํ•œ ์ฝ”๋“œ๊ฐ€ ํƒœ์–ด๋‚œ๋‹ค !

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