๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/React

[React] ๋ฆฌ์•กํŠธ์—์„œ ์™œ useState๋ฅผ ์“ฐ๋ผ๊ณ  ํ• ๊นŒ

by rindev 2021. 10. 26.

 

์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์•˜๋Š”๋ฐ, ์—„์ฒญ ์ƒ์„ธํ•ด์„œ ๋†€๋ž๋‹ค!

์—ญ์‹œ ํ•ด๋ด์•ผ ์•„๋Š” ๊ฒƒ๋“ค์ด ์•„์ฃผ ๋งŽ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ๋Š” ์š”์ฆ˜์ด๋‹ค๐Ÿ˜Ž

 

 

๐Ÿค” ๊ทธ๋ƒฅ ๋ณ€์ˆ˜ ๋ง๊ณ  state๋ฅผ ์“ฐ๋Š” ๊ฐ€์žฅ ํฐ ์ด์œ 

state๊ฐ€ ๋ณ€๊ฒฝ ๋  ๋•Œ ๊ด€๋ จ๋œ DOM์„ ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, state๋ฅผ ์จ์•ผ ํ•œ๋‹ค!

→ SPA๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์‹ถ์œผ๋ฉด state๋ฅผ ์จ์•ผํ•œ๋‹ค

 

์™œ๋ƒํ•˜๋ฉด ๋ฆฌ์•กํŠธ๋Š” state์— ์ˆ˜์ •์ด ์ผ์–ด๋‚˜๋ฉด state๊ฐ€ ํฌํ•จ๋œ DOM์„ ์žฌ๋ Œ๋”๋ง ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ....

 

๊ทธ๊ฑธ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฆฌ์•กํŠธ์˜ Life cycle์„ ์•Œ์•„์•ผ ํ•œ๋‹ค๐Ÿคฏ #๊ฐ€๋ณด์ž๊ณ 

 

 

๐Ÿค” setState๋ง๊ณ  state๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ์™œ ํ™”๋ฉด ๊ฐฑ์‹ ์ด ์•ˆ๋ ๊นŒ?

๋ฆฌ์•กํŠธ์˜ ์ปดํฌ๋„ŒํŠธ๋Š” Mount ์ƒํƒœ์—์„œ ํ•œ๋ฒˆ render() ๋ฅผ ์‹คํ–‰ํ•˜๊ณ ,

ํ›„์—๋Š” Update ์ƒํƒœ์— ์ง„์ž…ํ•˜์—ฌ shouldComponentUpdate === true ์ผ๋•Œ๋งŒ render() ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

๊ทธ๋ž˜์„œ ๋ณ€๊ฒฝํ•œ state๊ฐ€ ํ™”๋ฉด์— ๋ณด์ด๊ฒŒ ๋˜๋ ค๋ฉด Update์ƒํƒœ์— ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š”๋ฐ ๊ทธ๋Ÿฌ๋ ค๋ฉด...

- state ๋˜๋Š” Props๊ฐ€ ๋ณ€๊ฒฝ
- ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง
- forceUpdate ์‚ฌ์šฉ

ํ•ด์•ผํ•œ๋‹ค.

 

state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด update๊ฐ€ ๋ผ์•ผ ํ•˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€? ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ๋ฆฌ์•กํŠธ๊ฐ€ ์ด ๊ฐ’์ด ๋ณ€๊ฒฝ ๋๋‹ค๋Š” ๊ฒƒ์„ ํŒ๋‹จํ•˜๋Š” ๊ธฐ์ค€์ด ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ณ  ๋‚ด๋ถ€์˜ ๊ฐ’๋งŒ ๋ฐ”๋€Œ๋ฉด ๋ฐ”๋€๊ฒƒ์œผ๋กœ ์ธ์‹์„ ์•ˆํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

์ด์™€ ๊ด€๋ จํ•ด์„œ useState๊ฐ€ const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋„ ๊ฐ™์ด ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ,

const [count, setCount] = useState(0);

 

count ๋ณ€์ˆ˜๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฑด๋ฐ ์™œ let์ด ์•„๋‹๊นŒ? ๋ผ๊ณ  ์ƒ๊ฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง๋˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋‹ค์‹œ ์‹คํ–‰๋˜์–ด ์ƒˆ scope๋ฅผ ๋งŒ๋“ค๊ณ , count๋ณ€์ˆ˜๋„ ์ƒˆ๋กœ ๋งŒ๋“ค๊ฒŒ ๋˜๊ณ , ์ด๋Š” ์ด์ „ ๋ณ€์ˆ˜์™€ ์ƒ๊ด€์ด ์—†๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ const๋กœ ์„ ์–ธ๋œ count ๋ณ€์ˆ˜๋Š” ๋™์ผํ•œ scope์—์„œ ๋‹ค์‹œ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค!

 

 

๐Ÿ™„ ๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๊ธฐ์—๋Š”

DOM์„ ์กฐ์ž‘ํ•ด์•ผํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ผ๋ฐ˜ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•˜๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ๋Š” ์ผ๋ฐ˜ ๋ณ€์ˆ˜ ๋ง๊ณ  state๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค! ๋ผ๊ณ  ์•Œ๊ณ ์žˆ์—ˆ๋Š”๋ฐ ์ด๋Š” DOM๊ณผ ๊ด€๋ จ๋œ ๋ถ€๋ถ„์—์„œ์˜€๊ณ , ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ์ผ๋ฐ˜๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์„ ํƒ์ง€๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž! 

 

๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์•กํŠธ ํ›…์€... ํด๋กœ์ €๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค.

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://hewonjeong.github.io/deep-dive-how-do-react-hooks-really-work-ko/

 

 

๐Ÿ‘€ ์ฐธ๊ณ 

๋”๋ณด๊ธฐ

https://tried.tistory.com/47

 

[React] state ์‚ฌ์šฉ๋ฒ• ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

import React, { useState } from 'react'; // useState๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด import import './App.css' function App(){ let [๊ธ€์ œ๋ชฉ, ๊ธ€์ œ๋ชฉ๋ณ€๊ฒฝ] = useState( ['๋‚จ์ž ์ฝ”ํŠธ ์ถ”์ฒœ', '๊ฐ•๋‚จ ์šฐ๋™ ๋ง›์ง‘'] ); // state๋ฅผ..

tried.tistory.com

 

https://velog.io/@wjdcksdud29/React-setState-useState%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94-%EC%9D%B4%EC%9C%A0-TIL-53

 

[React] setState & useState๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  / TIL # 53

setState & useState ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋ฅผ ์•Œ์•„๋ณด์ž

velog.io

 

https://developerntraveler.tistory.com/59

 

[ React HOOK ] useState๊ฐ€ let ๋Œ€์‹  const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

React์—์„œ useState Hook์„ ์‚ฌ์šฉํ•  ๋•Œ, ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. const [name, setName] = useState('abc'); useState๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด name ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์žฌํ• ..

developerntraveler.tistory.com

 

https://dudghsx.tistory.com/18

 

hooks์—์„œ useState๊ฐ€ const๋กœ ์„ ์–ธ๋˜๋Š” ์ด์œ 

 ์นด์นด์˜คํ†ก ์˜คํ”ˆ ์ฑ„ํŒ…๋ฐฉ์— ๋”ฐ๋ฅด๋ฉด ์š”์ฆ˜์˜ ๊ธฐ์—…๋“ค์€ ํด๋ž˜์Šคํ˜•์ด ์•„๋‹Œ ํ›…์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ๋‚จ๋“คํ•˜๋Š”๊ฑด ๋‹ค ํ• ์ค„ ์•Œ์•„์•ผ์ง€.. ๊ทธ๋ž˜์„œ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฝ์–ด๋ณด๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ko.reactjs.org/docs/hooks

dudghsx.tistory.com

 

https://hewonjeong.github.io/deep-dive-how-do-react-hooks-really-work-ko/

 

[๋ฒˆ์—ญ] ์‹ฌ์ธต ๋ถ„์„: React Hook์€ ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ๊นŒ?

React Hook์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๋ ค๋ฉด JavsScript ํด๋กœ์ €์— ๋Œ€ํ•ด ์ž˜ ์•Œ์•„์•ผํ•ฉ๋‹ˆ๋‹ค. React์˜ ์ž‘์€ ๋ณต์ œ๋ณธ์„ ๋งŒ๋“ค์–ด๋ณด๋ฉฐ ํด๋กœ์ €์™€ hook์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค.

hewonjeong.github.io

 

 

 

๋Œ“๊ธ€