[ReactJS] Hook ๊ฐœ๋… ์•Œ์•„๋ณด๊ธฐ

2020. 6. 3. 15:26ใ†ETC/Programming

โ“ Hook์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  

hook์ด ๋‚˜์˜ค๊ธฐ ์ „์— ์‚ฌ๋žŒ๋“ค์€ Class๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ๋Š” hook์„ ํ†ตํ•ด state๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์—ฌ๋Ÿฌ react ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

hook ์ด์ „์—๋Š” HOC(Higher Order Component)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ž‘์„ฑ์„ ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

"HOC"์ด๋ž€ ํ™”๋ฉด์—์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋กœ์ง๋งŒ์„ ๋ถ„๋ฆฌํ•ด์„œ component๋กœ ๋งŒ๋“ค๊ณ , ์žฌ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•œ UI์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ parameter๋กœ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•˜๋Š” ํŒจํ„ด์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ HOC์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, wrapper hell์ด๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”!

"wrapper hell"์ด๋ž€ ์ค‘์ฒฉ๋˜๋Š” component๊ฐ€ ๋งŽ์•„์ ธ depth๊ฐ€ ๊นŠ์–ด์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

LifeCycle API๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ DOM ์œ„์— ์ƒ์„ฑ๋˜๊ธฐ ์ „ ํ›„ ๋ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฉด๊ฒฝ๋˜์–ด์„œ ์ƒ์ฑ„๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ „ ํ›„๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Class๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, componentDidMount, componentWillUnmount, componentWillUpdate ๋“ฑ์˜ LifeCycle API๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌ ํ•˜ํ•˜ ๊ฒŒ ๋˜์–ด ํ•จ์ˆ˜๋Š” ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์„ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋˜๊ณ , ์ฝ”๋“œ๋Š” ๋ณต์žกํ•ด์ง€๋ฉฐ, ํ…Œ์ŠคํŠธ๋Š” ์ ์ฐจ ์–ด๋ ค์›Œ์กŒ์Šต๋‹ˆ๋‹ค.

 

=> hook์€ ๋กœ์ง ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉด์„œ wrapper hells๋ฅผ ํ”ผํ•˜๊ณ , ๋ฆฌ์•กํŠธ copmonent life cycle์— ์ข…์†์ ์ด์ง€ ์•Š๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

 

 

 

 

 

๐Ÿ‘ Hook์˜ ํŠน์ง•

1. Hook์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ƒํƒœ ๊ด€๋ จ ๋กœ์ง์„ ์ถ”์ƒํ™”

    โžก๏ธ ๊ณ„์ธต ๋ณ€ํ™” ์—†์ด ์ƒํƒœ ๊ด€๋ จ ๋กœ์ง์„ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ !

 

2. Class ์—†์ด ๋ฆฌ์•กํŠธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

    โžก๏ธ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ€๋Šฅ !

 

3. ํด๋ž˜์Šค๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

    โžก๏ธ Class์™€ hook ์ค‘ ์„ ํƒ์  ์‚ฌ์šฉ ๊ฐ€๋Šฅ !

( ๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด, ์ด๋ฏธ ์งœ ๋†“์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ชจ์กฐ๋ฆฌ ์žฌ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅํ•˜์ง€ ์•Š๊ณ , ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ hook์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.)

 

 

 

 

 

๐Ÿ“– Hook ์‚ฌ์šฉ ๊ทœ์น™

1. ์ตœ์ƒ์œ„์—์„œ๋งŒ Hook์„ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    (๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ, ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜ ๋‚ด์—์„œ Hook ์‹คํ–‰ โŒ)   

// ๐Ÿ‘Ž๊ทœ์น™์„ ์–ด๊ธฐ๊ณ  ์žˆ์Œ.
if (name !== '') { 
    useEffect(function persistForm() {
        localStorage.setItem('formData', name);
    });
}
// ๐Ÿ‘์กฐ๊ฑด๋ถ€๋กœ ์‹คํ–‰ํ•˜๊ธฐ ์›ํ•  ๋•Œ
useEffect(function persistForm() {
    if (name !== '') {
		localStorage.setItem('formData', name);
	}
});
  

 

2. React ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ๋งŒ Hook์„ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    (custom hook์—์„œ๋Š” ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์ผ๋ฐ˜ JavaScript ํ•จ์ˆ˜์—์„œ๋Š” ํ˜ธ์ถœ โŒ)

 

 

 

 

 

 

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ป ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” React๊ฐ€ ์ œ๊ณตํ•˜๋Š” Hook API์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿง‘๐Ÿป‍๐Ÿ’ป

 

 

์ฐธ๊ณ  ๋งํฌ ๐Ÿ™

 

Hook์˜ ๊ฐœ์š” – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

Hook์˜ ๊ทœ์น™ – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

React์˜ ์ƒˆ๋กœ์šด ํŒจ๋Ÿฌ๋‹ค์ž„, React Hooks

React Hooks์ด ์ƒ๊ธด ๋ฐฐ๊ฒฝ๊ณผ Basic Hook์ธ useState, useEffect์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ , custom hook์— ๊ด€ํ•ด ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

velog.io

 

๋ฆฌ์•กํŠธ(React) ์ดํ•ด 4 - Higher Order Component(HOC)๋กœ ์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉ ํ•˜๊ธฐ

๊ฐœ์ธ ๋ธ”๋กœ๊ทธ๋กœ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ ๋ฆฌ์•กํŠธ(React) ์ดํ•ด 4 - Higher Order Component(HOC)๋กœ ์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉ ํ•˜๊ธฐ์—์„œ ๊ธ€ ๋ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ƒˆ๋กœ์šด ๊ณณ์—์„œ ๋ฆฌ์•กํŠธ ๊ด€๋ จํ•œ ์œ ์šฉ...

www.vobour.com