[React] React Hook์ด๋ž€?

@Joy Lee ยท December 22, 2021 ยท 4 min read

๐ŸŒŸ Hook์˜ ๋“ฑ์žฅ ๋ฐฐ๊ฒฝ

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ ์ข…๋ฅ˜ : Class Component / Functional Component

Hook์ด ๋“ฑ์žฅํ•˜๊ธฐ ์ „ ๋ฆฌ์•กํŠธ์—๋Š” ์—ฌ๋Ÿฌ ๋ฌธ์ œ๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. Class Component๋Š” Functional Component์— ๋น„ํ•ด ๋ฌธ๋ฒ•์ด ์–ด๋ ต๊ณ  ๋กœ์ง์˜ ์žฌ์‚ฌ์šฉ์ด ํž˜๋“  ๋‹จ์ ์ด ์žˆ์œผ๋‚˜ Functional Component๋กœ๋Š” ํ•  ์ˆ˜ ์—†์—ˆ๋˜ state๊ด€๋ฆฌ๋‚˜ Life Cycle Method๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ด์œ ๋กœ ์–ด์ฉ” ์ˆ˜ ์—†์ด Class Component๋ฅผ ์‚ฌ์šฉํ•ด์™”์Šต๋‹ˆ๋‹ค.

Hook์ด ๋“ฑ์žฅํ•˜๊ธฐ ์ „์˜ ๋ฆฌ์•กํŠธ๋Š”

  • ์ปดํฌ๋„ŒํŠธ ์‚ฌ์ด์—์„œ ์ƒํƒœ์™€ ๊ด€๋ จ๋œ ๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค.

  • ์ƒ๋ช…์ฃผ๊ธฐ ์ปดํฌ๋„ŒํŠธ๋กœ ์ธํ•œ ๋ณต์žกํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค. ์ƒํƒœ๊ด€๋ จ ๋กœ์ง์ด ๋ชจ๋‘ ๊ฐ™์€ ๊ณต๊ฐ„์— ์œ„์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๐Ÿง Hook ์ด๋ž€?

Hook์€ ๋ฆฌ์•กํŠธ 16.8 ๋ฒ„์ „ ์ดํ›„ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์— ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ฆ‰, React Hook์€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋ฉฐ Hook์ด ๋“ฑ์žฅํ•˜๋ฉด์„œ ๋” ์ด์ƒ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Class Component๋ฅผ ์“ธ ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด์—๋Š” Class Component ์—์„œ๋งŒ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Hook์ด ๋“ฑ์žฅํ•˜๋ฉด์„œ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์—์„œ๋„ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ณ , ์ƒํƒœ ๊ด€๋ฆฌ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ธฐ์กด ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ React Hook ๊ณต์‹๋ฌธ์„œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿงพ Hook์˜ ์ข…๋ฅ˜

useState

๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ Hook์œผ๋กœ ์ƒํƒœ๊ฐ’์„ ์ƒ์„ฑ ๋ฐ ์—…๋ฐ์ดํŠธ์‹œ์ผœ์ฃผ๋Š” Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useState ๋” ์•Œ์•„๋ณด๊ธฐ

useEffect

์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋  ๋•Œ๋งˆ๋‹ค ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useEffect ๋” ์•Œ์•„๋ณด๊ธฐ

useReducer

useState์ฒ˜๋Ÿผ ์ƒํƒœ๊ด€๋ฆฌ์— ์“ฐ์ด๋ฉฐ ๊ตฌ์กฐ๊ฐ€ ๋ณต์žกํ•œ ์ƒํƒœ๊ด€๋ฆฌ์— ์ ํ•ฉํ•œ Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useReducer ๋” ์•Œ์•„๋ณด๊ธฐ

useRef

๋ Œ๋”๋ง๊ณผ ๊ด€๋ จ์—†๋Š” ๊ฐ’์„ ๊ด€๋ฆฌํ•˜๊ฑฐ๋‚˜ DOM์— ์ ‘๊ทผ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useRef ๋” ์•Œ์•„๋ณด๊ธฐ

useMemo

๋ Œ๋”๋ง ์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ๊ฐ’์„ ์ €์žฅํ•ด๋‘๊ณ  ํ•„์š”ํ•  ๋•Œ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useMemo ๋” ์•Œ์•„๋ณด๊ธฐ

useCallback

useMemo์™€ ๋น„์Šทํ•˜๋‚˜ ๊ฐ’์ด ์•„๋‹Œ ํ•จ์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useCallback ๋” ์•Œ์•„๋ณด๊ธฐ

useContext

์ „์—ญ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” state๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„์š”ํ•œ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๋Š” Hook์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ useContext ๋” ์•Œ์•„๋ณด๊ธฐ


REFERENCE
React(23) ๋ฆฌ์•กํŠธ ํ›…์ด๋ž€? https://devbirdfeet.tistory.com/52
๋ฆฌ์•กํŠธ ๊ณต์‹ ๋ฌธ์„œ https://reactjs.org/docs/hooks-intro.html
[React] Hooks์˜ ์ข…๋ฅ˜ https://lelana.tistory.com/146

Joy Lee
FRONTEND DEVELOPER