๐Ÿ›‘ React ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜์ง€ ๋งˆ์„ธ์š”.

3440 ๋‹จ์–ด reactjavascriptwebdevprogramming
์ด ๊ฒŒ์‹œ๋ฌผ์—์„œ๋Š” ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ์—์„œ ReactJs์˜ ์ƒํƒœ ๋™๊ธฐํ™” ๋ฐ ํ›„ํฌ๋กœ์˜ ํŒจ๋Ÿฌ๋‹ค์ž„ ์ „ํ™˜์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Here is a video version of this post, if that's what you prefer.



ํด๋ž˜์Šค ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ReactJs์— ์žˆ์„ ๋•Œ(์•„์ง๋„ ๊ทธ๋ ‡์ง€๋งŒ ์•„๋ฌด๋„ ๋” ์ด์ƒ ์ข‹์•„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค) ์šฐ๋ฆฌ๋Š” ๋งˆ์šดํŠธ, ๋งˆ์šดํŠธ ํ•ด์ œ ๋ฐ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ฐฉ๋ฒ• ์ธก๋ฉด์—์„œ ๋งŽ์ด ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.



ํด๋ž˜์Šค ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋งˆ์šดํŠธ๋  ๋•Œ๋งˆ๋‹ค ์ƒ์„ฑ์ž โ†’ ๋ Œ๋”๋ง โ†’ DOM ๋ฐ refs ์—…๋ฐ์ดํŠธ โ†’ componentDidMount ์ˆœ์„œ๋กœ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ React Hooks๊ฐ€ ๋“ฑ์žฅํ–ˆ๊ณ  ์šฐ๋ฆฌ๋Š” ์ข…์†์„ฑ ๋ฐฐ์—ด ์ธก๋ฉด์—์„œ ์ƒ๊ฐํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ข…์ข… ์šฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌป์Šต๋‹ˆ๋‹ค.

What is the hooksย equivalentย toย [some lifecycle method]?



๋น ๋ฅธ ๋Œ€๋‹ต์€ ํ›„ํฌ๊ฐ€ "๋ผ์ดํ”„์‚ฌ์ดํด"๊ด€์ ์—์„œ "์ƒํƒœ ๋ฐ DOM๊ณผ์˜ ๋™๊ธฐํ™”"๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ํŒจ๋Ÿฌ๋‹ค์ž„ ์ „ํ™˜์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.



์ด์ „ ํŒจ๋Ÿฌ๋‹ค์ž„์„ ์ทจํ•˜์—ฌ ํ›„ํฌ์— ์ ์šฉํ•˜๋ ค๋Š” ์‹œ๋„๋Š” ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋ฐฉํ•ด๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

useEffect(fn) // fn invoked on all updates

useEffect(fn, []) // invoked on mount

useEffect(fn, [a, b, c]) // invoked if any of the members of the array are updated


์œ„์˜ ์Šค๋‹ˆํŽซ์€ React ํ›„ํฌ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค.

๋ผ์ดํ”„์‚ฌ์ดํด์€ ์‚ฌ๋ผ์กŒ์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ์—ฌ์ „ํžˆ ๋นˆ dep ๋ฐฐ์—ด์ด ์žˆ๋Š” useEffect๋ฅผ componentDidMount๋กœ ์ƒ๊ฐํ•˜๋ฉฐ ์ด๋Š” React Hooks์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ์‹์ด ์•„๋‹™๋‹ˆ๋‹ค.

์ด์ œ ๋ฐ˜์‘ ํ›„ํฌ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

์ƒํƒœ ๋™๊ธฐํ™”



๋ณด์„ธ์š”, ๋ฌธ์ œ๋Š” "์ด ํšจ๊ณผ๊ฐ€ ์–ธ์ œ ์‹คํ–‰๋ฉ๋‹ˆ๊นŒ?"๊ฐ€ ์•„๋‹ˆ๋ผ "์ด ํšจ๊ณผ๊ฐ€ ๋™๊ธฐํ™”๋˜๋Š” ์ƒํƒœ"์ž…๋‹ˆ๋‹ค.

useEffect(fn) // useEffect with no dep array, sync with all state

useEffect(fn, []) // useEffect with empty dep array, sync with no state

useEffect(fn, [stateA, stateB]) // useEffect with stateA and stateB in dep array sync with stateA and stateB.


์ด๊ฒƒ์ด ๋ฐ”๋กœ React Hooks์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด์•ผ ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.


์ด ์˜์ƒ์ด ๋„์›€์ด ๋˜์…จ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ตฌ๋…์„ ํ†ตํ•ด ์ง€์›์„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

์ €๋Š” ๋˜ํ•œ ์ฃผ๊ฐ„ ๋‰ด์Šค๋ ˆํ„ฐ๋ฅผ ์šด์˜ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์—ฌ๊ธฐ์—์„œ๋„ ์ €์™€ ํ•จ๊ป˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: https://www.getrevue.co/profile/8020lessons

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ