[React] ๋ฆฌ์•กํŠธ State

yayaya_hojinยท2023๋…„ 9์›” 5์ผ
0

๋ฆฌ์—‘ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-thumbnail

๐Ÿš€ React์—์„œ State ๊ด€๋ฆฌํ•จ์œผ๋กœ์จ props drilling์„ ํ”ผํ•œ๋‹ค

State ์ข…๋ฅ˜

React์—์„œ State๋Š” ํฌ๊ฒŒ ์ง€์—ญ ์ƒํƒœ, ์ „์—ญ ์ƒํƒœ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒŸ Local State(์ง€์—ญ ์ƒํƒœ)

์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ์ƒํƒœ

ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” props๋ฅผ ์‚ฌ์šฉ

๐ŸŒŸ Global State(์ „์—ญ ์ƒํƒœ)

ํ”„๋กœ์ ํŠธ ์ „์ฒด์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ์ƒํƒœ

์ „์—ญ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋งŒํผ ์ƒํƒœ์˜ ์œ„์น˜์— ์ƒ๊ด€์—†์ด ์‚ฌ์šฉ ๋ฐ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ


State ๋ถˆ๋ณ€์„ฑ ์œ ์ง€

๋ถˆ๋ณ€์„ฑ์ด๋ž€?

๏ฅง(์•„๋‹ ๋ถˆ) ่ฎŠ(๋ณ€ํ•  ๋ณ€) ํ• ๋‹นํ•œ ๊ฐ’์ด ๋ณ€ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๋œป

React ์—์„œ React Hook useState๋กœ ํ• ๋‹นํ•œ ๊ฐ’์„ setState๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์—†๋‹ค.

JS ์—”์ง„์€ ์ฝœ ์Šคํƒ(Call Stack)๊ณผ ๋ฉ”๋ชจ๋ฆฌ ํž™(Memory Heap) 2๊ฐ€์ง€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์ฝœ ์Šคํƒ(Call Stack)์—๋Š” String, Boolean, Number ๋“ฑ ์›์‹œ ํƒ€์ž…์ด ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„์ด๊ณ 

๋ฉ”๋ชจ๋ฆฌ ํž™(Memory Heap)์€ Array, Object ๋“ฑ์ด ์ฐธ์กฐ ํƒ€์ž…์ด ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„์ด๋‹ค

before_img

์›์‹œํƒ€์ž…์€ Call Stack์— ๊ฐ’์„ ์ €์žฅ

์ฐธ์กฐํƒ€์ž…์˜ ์‹ค์ œ ๊ฐ’์€ Memory Heap์— ์ €์žฅ ๋˜๊ณ  Memory Heap ์ฃผ์†Œ๊ฐ€ Call Stack์— ์ €์žฅ

after_img

๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ฒŒ๋˜๋ฉด

์›์‹œํƒ€์ž…์€ Call Stack์— ์ƒˆ๋กœ์šด ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๊ฐ’์„ ์ €์žฅ

์ฐธ์กฐํƒ€์ž…์€ Call Stack์˜ ์ฃผ์†Œ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ํ•ด๋‹น Memory Heap์— ๊ฐ’์„ ๋ณ€๊ฒฝ

๊ทธ๋Ÿฐ๋ฐ React๋Š” state์˜ ๋ณ€ํ™”๋ฅผ ์–•์€ ๋น„๊ต๋กœ Call Stack์˜ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•ด ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€

์›์‹œํƒ€์ž…์˜ ๊ฒฝ์šฐ ๋ณ€ํ™” ๊ฐ์ง€๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ

์ฐธ์กฐํƒ€์ž…์˜ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์ฐธ์กฐํƒ€์ž…์„ ๋งŒ๋“ค์ง€ ์•Š์œผ๋ฉด ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•˜์ง€ ๋ชปํ•จ

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๋‚˜?

KeepState.js

import React, { useState } from "react";

const KeepState = () => {
  const [arr, setArr] = useState([1, 2, 3, 4]);
  const handleClickBtn = () => {
    setArr([...arr, 5]);
  };
  console.log(arr);
  return <button onClick={handleClickBtn}>Button</button>;
};

export default KeepState;

console.log

๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด handleClickBtn ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด

๊ธฐ์กด์— ์ €์žฅ๋œ ๋ฐฐ์—ด ...(์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•)arr์„ ์‚ฌ์šฉํ•ด ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ

setArr๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์˜ ๊ฐ’์œผ๋กœ ํ• ๋‹นํ•ด์ค€๋‹ค.

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๊ณ  ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.


setState ๋™๊ธฐ or ๋น„๋™๊ธฐ?

๋™๊ธฐ(Synchronous) - ์š”์ฒญ์„ ๋ณด๋‚ธ ํ›„ ์‘๋‹ต์„ ๋ฐ›์•„์•ผ ๋‹ค์Œ ๋™์ž‘์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐฉ์‹, ๋ชจ๋“  ์ผ์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋˜๋ฉฐ ํŠน์ • ์ž‘์—…์ด ์ˆ˜ํ–‰ ์ค‘์ด๋ผ๋ฉด ๋‹ค์Œ ์ž‘์—…์€ ๋Œ€๊ธฐํ•œ๋‹ค.

๋น„๋™๊ธฐ(Asynchronous) - ๋จผ์ € ์‹คํ–‰๋œ ์ž‘์—…์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด setState๋Š” ๋น„๋™๊ธฐ๋กœ ์ž‘๋™ํ•œ๋‹ค.

Counter.js

import React, { useState } from "react";

const Counter = () => {
  const [count, setCount] = useState(0);
  const handleClickBtn = () => {
    setCount(count + 1);
    console.log(count);
  };
  return (
    <div>
      <div>{count}</div>
      <button onClick={handleClickBtn}>+1</button>
    </div>
  );
};

export default Counter;

์œ„ ์ฝ”๋“œ์—์„œ ๋ฒ„ํŠผ์„ 3๋ฒˆ ๋ˆ„๋ฅด๋ฉด console์— 1, 2, 3 ์ด๋ ‡๊ฒŒ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์˜ˆ์ƒํ•œ๋‹ค.

console.log

ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ ๊ฐ’๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ 0, 1, 2๊ฐ€ ๋‚˜์™”๋‹ค.

๊ทธ ์ด์œ ๋Š” setCount๊ฐ€ ๋น„๋™๊ธฐ๋กœ ์ž‘๋™์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

React์—์„œ๋Š” State ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚˜๋ฉด ๋ณ€๊ฒฝ๋œ ๊ฐ’๋“ค์„ ๋ชจ์•„ ํ•œ๋ฒˆ์— ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ๋ Œ๋”๋ง์„ ์ค„์ด๊ธฐ ํœ˜ํ•œ Batch ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ’ฅ Batch์˜ ์—…๋ฐ์ดํŠธ๋Š” 16ms ์ฃผ๊ธฐ์ด๋‹ค!

๋งŒ์•ฝ State๊ฐ€ ๋งŽ๊ณ  ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚  ๋•Œ๋งˆ๋‹ค ๋ Œ๋”๋ง์„ ํ•˜๊ฒŒ ๋˜๋ฉด React๋Š” ๋งค๋ฒˆ ๋ Œ๋”๋ง๋งŒ ํ•˜๊ณ  ๊ฒฐ๊ตญ ์†๋„๊ฐ€ ๋Š๋ ค์ง€๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Batch ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์ด ๋•Œ๋ฌธ์— setCount๊ฐ€ ๋น„๋™๊ธฐ๋กœ ์ž‘๋™์„ ํ•ด ๊ทธ ํ›„์— ์˜ค๋Š” console.log๊ฐ€ ์‹คํ–‰์ด ๋˜๊ณ  ๊ทธ ํ›„ setCount๊ฐ€ ์ž‘๋™์„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

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