ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

https://ko.reactjs.org/docs/hooks-state.html

 

 

 

 

Hook๊ณผ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ - ๋‘˜ ๋‹ค ๊ฐ€๋Šฅ

const Example = (props) => {
  // ์—ฌ๊ธฐ์„œ Hook์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
  return <div />;
}
function Example(props) {
  // ์—ฌ๊ธฐ์„œ Hook์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
  return <div />;
}
  • state๊ฐ€ ์—†๋Š” ์ปดํฌ๋„ŒํŠธ
  • hook์€ state๋ฅผ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์คŒ
  • hook์€ ํด๋ž˜์Šค ์•ˆ์—์„œ ๋™์ž‘ X
  • ํ•˜์ง€๋งŒ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

 

 

Hook์ด๋ž€?

ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜.

์˜ˆ๋ฅผ ๋“ค์–ด useState๋Š” state๋ฅผ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ

 

 

์–ธ์ œ Hook ์‚ฌ์šฉ?

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ์— state๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

 

 

 

 

state ๋ณ€์ˆ˜ ์„ ์–ธ

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” this๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— this.state๋ฅผ ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ์ฝ์„ ์ˆ˜ ์—†์Œ.

๋Œ€์‹  useState Hook์„ ์ง์ ‘ ์ปดํฌ๋„ŒํŠธ์— ํ˜ธ์ถœ!

 

import React, { useState } from 'react';

function Example() {
  // ์ƒˆ๋กœ์šด state ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , ์ด๊ฒƒ์„ count๋ผ ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.
  const [count, setCount] = useState(0);

 

 

 useState 

useState๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ๋ฌด์—‡์„ ํ•˜๋‚˜?

-> state ๋ณ€์ˆ˜ ์„ ์–ธ

  • ์œ„ ์ฝ”๋“œ์—์„œ count (์•„๋ฌด ์ด๋ฆ„์œผ๋กœ ์ง€์–ด๋„ ๋จ)
  • useState๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ this.state ๊ธฐ๋Šฅ๊ณผ ๊ฐ™์Œ
  • ์ผ๋ฐ˜ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋๋‚  ๋•Œ ์‚ฌ๋ผ์ง€์ง€๋งŒ, state ๋ณ€์ˆ˜๋Š” React์— ์˜ํ•ด ์‚ฌ๋ผ์ง€์ง€ ์•Š์Œ

 

useState์˜ ์ธ์ž๋กœ ๋ฌด์—‡์„ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•˜๋‚˜?

-> state์˜ ์ดˆ๊ธฐ๊ฐ’

  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์˜ state๋Š” ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ ๊ฐ์ฒด์ผ ํ•„์š”x, ์ˆซ์ž ํƒ€์ž…๊ณผ ๋ฌธ์ž ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.
  • 2๊ฐœ์˜ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ์‹ถ์œผ๋ฉด useState() ๋‘ ๋ฒˆ ํ˜ธ์ถœ

 

useState๋Š” ๋ฌด์—‡์„ ๋ฐ˜ํ™˜?

-> state ๋ณ€์ˆ˜, ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ (์Œ์œผ๋กœ ๋ฐ˜ํ™˜)

  • ex) const [count, setCount] = useState()

 

import React, { useState } from 'react';

function Example() {
  // ์ƒˆ๋กœ์šด state ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , ์ด๊ฒƒ์„ count๋ผ ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.
  const [count, setCount] = useState(0);
  
  • count๋ผ๋Š” state๋ณ€์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
  • count๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋ ค๋ฉด setCount ํ˜ธ์ถœ

 

 

 

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

  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ
    • {this.state.count}
  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ
    • {count}

 

 

 

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

  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ
    • this.setState()
  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ
    • setCount()
    • setCount์™€ count๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ this ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋จ

 

 

 

์š”์•ฝ

import React, { useState } from 'react';
//useState ํ›…์„ React์—์„œ ๊ฐ€์ ธ์˜ด

function MyComponent() {
    const [count, setCount] = useState(0);
	// state๋ณ€์ˆ˜ count์™€, state๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” setCount ํ•จ์ˆ˜ ์ƒ์„ฑ
    // useState์˜ ์ธ์ž๊ฐ’์œผ๋กœ 0์„ ๋„˜๊ฒจ์ฃผ๋ฉด count = 0์œผ๋กœ ์ดˆ๊ธฐํ™”
    
    return (
        <div>
            <p>You click {count} times </p>
            <button onClick={() => setCount(count + 1)}>
                Click me
            </button>
        </div>
		// ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ ํด๋ฆญ ํ•˜๋ฉด setCountํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ state๋ณ€์ˆ˜(count) ๊ฐฑ์‹ 
		// React๋Š” ์ƒˆ๋กœ์šด count๋ณ€์ˆ˜๋ฅผ MyComponent์— ๋„˜๊ธฐ๋ฉฐ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌ๋ Œ๋”๋ง
    );
}

export default MyComponent;

 

 

 

 

 

๋

๋Œ“๊ธ€