<React> ๐Ÿ’Ž Hook

๊น€๋ฏผ์„ยท2021๋…„ 2์›” 25์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
14/18
post-thumbnail

Hook์ด React ๋ฒ„์ „ 16.8์— ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Hook์„ ์ด์šฉํ•˜์—ฌ Class๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด ์ƒํƒœ ๊ฐ’๊ณผ ์—ฌ๋Ÿฌ React์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Hook์€ Class์—†์ด React ๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

React Hook์€ function Component์—์„œ state์™€ life cycle method๋“ฑ์˜ react ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. React์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ํ›…๋“ค์€ ๋Œ€๋ถ€๋ถ„ use๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค.

  • function Componet์™€ class Component ์ฐจ์ด
    • state์™€ props ๋ณ€๊ฒฝ์‹œ ํ˜ธ์ถœ ๋ฒ”์œ„
      • function
        ํ•จ์ˆ˜ ์ „์ฒด๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ์ฆ‰ ์ง€์—ญ ๋ณ€์ˆ˜ ํ•จ์ˆ˜ ๋ชจ๋‘ ํ˜ธ์ถœ๋œ๋‹ค.
      • class
        render ํ•จ์ˆ˜๋งŒ ํ˜ธ์ถœ๋œ๋‹ค. ์ฆ‰, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.

Hook์„ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•œ class Component๋ฅผ Hook์„ ์ด์šฉํ•ด์„œ function Component๋กœ ๋ณ€๊ฒฝํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ

๊ผญ class Component ๋Œ€์‹  function Component๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋”ํ•˜์—ฌ ๊ธฐ์กด์— class Component๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋‹ค๋งŒ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด ๊ผญ class Component๊ฐ€ ํ•„์š”ํ•œ ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด function Component๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

class Component(๋ณ€๊ฒฝ ์ „)

์•„๋ž˜ ์ ํžŒ ์ฝ”๋“œ๋Š” +๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ˆซ์ž๋ฅผ 1์”ฉ ํ‚ค์›Œ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ Component๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

import React, {Component} from 'react';

class Plus{
  state = {
    number: 0
  }

  plus = () => {
    this.setState({number:this.state.number+1})
  }
  render(){
    const {number} = this.state;
    return(
      <>
        <h1>{number}</h1>
        <button onClick={this.plus}>+</button>
      </>
    );
  }
}

export default Plus;

function Component(๋ณ€๊ฒฝ ํ›„)

  • useState
    state๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด useState๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. useState์— ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ ๋ฐฐ์—ด์—๋Š” ๊ฐ’๊ณผ ๊ทธ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. state๋‚˜ props๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด function Component์˜ ํŠน์„ฑ์ƒ useState๋„ ๋‹ค์‹œ ์‹คํ–‰๋˜์ง€๋งŒ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„์˜ค์ง„ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋”ฐ๋กœ ์ €์žฅ์„ ํ•ด๋‘๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  • useRef
    Ref๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋˜๋Š” ์ƒํ™ฉ์—์„œ createRef๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งค๋ฒˆ ์ƒˆ๋กœ์šด reference๋ฅผ ๋งŒ๋“ค์–ด ์ข‹์ง€ ์•Š์œผ๋ฏ€๋กœ useRef๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    ํ˜„์žฌ ์ƒํ™ฉ์—์„œ ref๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”์—†์ง€๋งŒ ์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด ์ ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ref๋ฅผ ๊ตณ์ด ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด e.target์œผ๋กœ ํ•ด๊ฒฐํ•œ๋‹ค๋˜๊ฐ€..
  • useCallback
    button์˜ props๋กœ ์‚ฌ์šฉ๋œ ์ฝœ๋ฐฑํ•จ์ˆ˜ plus๋„ ๋งค๋ฒˆ ์ƒˆ๋กœ์šด reference๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ plus ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์–ด๋–ค ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ ์ง์ ‘์ ์œผ๋กœ ์กฐํšŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” useCallback์„ ์ด์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. useCallback์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•ด๋‘๊ณ  class Component์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋™์ผํ•œ reference์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์ค๋‹ˆ๋‹ค.
import React, {useState} from 'react';

const Plus = () => {
  const [number, setNumber] = useState(0);
  const numRef = useRef();
  
  const plus = useCallback(() => {
    setNumber(number+1);
  });
  
  return (
    <>
      <h1 ref={numRef}>{number}</h1>
      <button onClick={plus}>+</button>
    </>
  );
}
profile
๋ˆ„๊ตฌ๋‚˜ ์‹ค์ˆ˜ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ €๋Š” ๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋˜๊ณ  ์‹ถ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•˜์—ฌ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค.๐Ÿ™ƒ

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