๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Code-States

[D+81] React Hook

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 1. 8.

 

 

D+81

-  React Hook -

(React Hook)

 


 

 

React Hook

๊ธฐ์กด์— ์šฐ๋ฆฌ๋Š” ๋ฆฌ์•กํŠธ์—์„œ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” function์ด ์•„๋‹Œ Class๋ฅผ ์‚ฌ์šฉํ•ด ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด ์™”๋‹ค.

 

Class๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์˜ค๋ฉด์„œ ๋Š๋‚€์ ์€ ์ดˆ๊ธฐ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ  ํŠน์ • ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒํƒœ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ๋ถˆ๋Ÿฌ์˜จ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฝค๋‚˜ ๋ณต์žกํ•œ ๊ณผ์ •์ด๋ผ๋Š” ๊ฒƒ์„ ๋Š๊ผˆ๋‹ค.

 

๋ฟ๋งŒ this์— ๋Œ€ํ•œ ๊ฐœ๋…์ด ์–ด๋Š์ •๋„ ์žˆ์–ด์•ผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— bind๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๊ณ ,

this๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํฐ ์‹ค์ˆ˜ ์—†์ด ์ƒํƒœ๋ฅผ ์ œ๋Œ€๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ๊ฝค ๋ถˆํŽธํ•œ ์ ์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•ด Hook์ด ๋“ค์–ด์˜ค๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ

์–ด๋–ป๊ฒŒ Hook์„ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

 

useState

import React, { useState } from 'react';

function Example() {
  // ...
}

๋จผ์ € ์šฐ๋ฆฌ๊ฐ€ ํ›…์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” useState๋ฅผ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

import React, { useState } from 'react';

function Example() {
  // [state ๋ณ€์ˆ˜ : count , state ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ : setCount]
  // ์ดˆ๊ธฐ๊ฐ’ : 0
  const [count, setCount] = useState(0);

useState๋Š” ์šฐ๋ฆฌ๊ฐ€ ํด๋ž˜์Šค ํ•จ์ˆ˜์—์„œ this.state๋กœ ์ดˆ๊ธฐ ๊ฐ’์„ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋“ฏ์ด

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ useState์•ˆ์— ํŠน์ • ๊ฐ’์„ ๋„ฃ์–ด ์ดˆ๊ธฐ ๊ฐ’์„ ์„ค์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋•Œ Class์—์„œ this.state๋ผ๋Š” ๊ฐ์ฒด ์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด ์ฃผ์—ˆ์ง€๋งŒ

ํ›…์Šค๋Š” "[ ]"์•ˆ์— "[๋ณ€์ˆ˜, ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜]" ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

state ๊ฐ€์ ธ์˜ค๊ธฐ

// Class Component
<p> You clicked {this.state.count} times </p>


// Funtion Component
<p> You clicked {count} times </p>

๊ธฐ์กด์— ์šฐ๋ฆฌ๊ฐ€ ํŠน์ • ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ this.state๋ฅผ ์ด์šฉํ•ด ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค์ง€๋งŒ

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์ธ ํ›…์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ดˆ๊ธฐ์— ์„ค์ •ํ•œ ์ƒํƒœ๋ณ€์ˆ˜๋งŒ ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค.

 

state ๊ฐฑ์‹ ํ•˜๊ธฐ

// Class Component
  <button onClick={() => this.setState({ count: this.state.count + 1 })}>
    Click me
  </button>



// Funtion Component
<button onClick={() => setCount(count + 1)}>
    Click me
  </button>

์ƒํƒœ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ๊ฐฑ์‹ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ this.setState๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•ด ์ฃผ์—ˆ์ง€๋งŒ,

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ƒํƒœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด ์ฃผ์—ˆ์„๋•Œ ์ƒํƒœ ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•ด ์ฃผ์—ˆ์œผ๋ฏ€๋กœ

ํ•ด๋‹น ํ•จ์ˆ˜์—๋‹ค๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์–ด ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๊ฑฐ๋‚˜ ๊ฐฑ์‹ ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

Effect Hook

import React, { useState, useEffect } from 'react';

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

  // componentDidMount, componentDidUpdate์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ
  useEffect(() => {
    // ๋ธŒ๋ผ์šฐ์ € API๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์„œ ํƒ€์ดํ‹€์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
    document.title = `You clicked ${count} times`;
  });

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Effect Hook์€ ์šฐ๋ฆฌ๊ฐ€ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ผ์ดํ”„ ์‚ฌ์ดํด์ธ ์ƒ๋ช…์ฃผ๊ธฐ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ๊ฒƒ๋“ค ๋Œ€์‹ ์—

useEffect๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

(effect hook์€ ์•„์ง ์ œ๋Œ€๋กœ ํ•™์Šตํ•˜์ง€ ๋ชปํ•ด ๋” ๊ณต๋ถ€ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.)

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€