๐ŸชuseState(), React Hook

์ง€์€ยท2022๋…„ 10์›” 4์ผ
0

โš›๏ธ React

๋ชฉ๋ก ๋ณด๊ธฐ
6/23
post-custom-banner

React Hook

React ๋ฒ„์ „ 16.8๋ถ€ํ„ฐ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์œผ๋กœ, Hook์„ ์ด์šฉํ•˜๋ฉด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด state์™€ ๊ฐ™์€ ์—ฌ๋Ÿฌ React ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Hook์€ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ React state๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜์ด๋‹ค.
  • Hook์€ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ๋งŒ Hook์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

Hook์˜ ๊ฐœ์š” (๊ณต์‹ ๋ฌธ์„œ)

Hook ๊ทœ์น™

1. ์ตœ์ƒ์œ„์—์„œ๋งŒ Hook์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
๋ฐ˜๋ณต๋ฌธ์ด๋‚˜ ์กฐ๊ฑด๋ฌธ, ๋˜๋Š” ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜ ๋‚ด์—์„œ Hook์„ ํ˜ธ์ถœํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

2. ์˜ค์ง React ํ•จ์ˆ˜ ๋‚ด์—์„œ Hook์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
์ผ๋ฐ˜์ ์ธ JavaScript ํ•จ์ˆ˜์—์„œ Hook์„ ํ˜ธ์ถœํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
Hook์€ React์˜ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์‚ฌ์šฉ๋˜๋„๋ก ๋งŒ๋“ค์–ด์ง„ ๋ฉ”์†Œ๋“œ์ด๋‹ค.

Hook ๊ทœ์น™


State Hook

: ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ณ€ํ™”ํ•˜๋Š” ๊ฐ’์„ state๋กœ ์ •ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด state๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋˜์–ด๋„ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค.

State Hook์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” React์—์„œ useState๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•œ๋‹ค.

import { useState } from "react";

useState()

  • useState()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ state ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    ์ผ๋ฐ˜์ ์ธ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์‚ฌ๋ผ์ง€์ง€๋งŒ, state ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜์–ด๋„ React์— ์˜ํ•ด ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.
  • useState() Hook์˜ ์ธ์ž๋กœ state ์ดˆ๊ธฐ ๊ฐ’์„ ๋„˜๊ฒจ์ค˜์•ผ ํ•œ๋‹ค. ex) 0, [], false ...
const [state ์ €์žฅ ๋ณ€์ˆ˜, state ๊ฐฑ์‹  ํ•จ์ˆ˜] = useState(state ์ดˆ๊ธฐ ๊ฐ’);
  • useState๋Š” state ๋ณ€์ˆ˜์™€, ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    (๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด useState๋กœ ํ˜ธ์ถœ๋œ ์ด ๋ณ€์ˆ˜๋“ค์„ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด๋‹ค.)
function Checkbox() {
  const [isChecked, setIsChecked] = useState(false);
}

๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด

const [fruit, setFruit] = useState('banana');

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์ด์šฉํ•œ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ’€์–ด์„œ ์“ฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

const fruitStateVariable = useState('banana'); // ๋‘ ๊ฐœ์˜ ์•„์ดํ…œ์„ ๋ฐ˜ํ™˜
const fruit = fruitStateVariable[0]; // ์ฒซ ๋ฒˆ์งธ ์•„์ดํ…œ
const setFruit = fruitStateVariable[1]; // ๋‘ ๋ฒˆ์งธ ์•„์ดํ…œ

โžก๏ธ 0์ด๋‚˜ 1๋กœ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค.


state๊ฐ€ ๊ฐฑ์‹ ๋˜๋Š” ๊ณผ์ •

function CheckboxExample() {
  const [isChecked, setIsChecked] = useState(false);

  const handleChecked = (event) => {
    setIsChecked(event.target.checked);
  };

  return (
    <div className="App">
      <input type="checkbox" checked={isChecked} onChange={handleChecked} />
      <span>{isChecked ? "Checked!!" : "Unchecked"}</span>
    </div>
  );
}
  1. ์‚ฌ์šฉ์ž๊ฐ€ ์ฒดํฌ๋ฐ•์Šค ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด onChange ์ด๋ฒคํŠธ๊ฐ€ handleChecked ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  2. ์ด ํ•จ์ˆ˜๊ฐ€ setIsChecked ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ํ˜ธ์ถœ๋œ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ isChecked ๋ณ€์ˆ˜๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.
  3. React๋Š” ์ƒˆ๋กœ์šด isChecked ๋ณ€์ˆ˜๋ฅผ CheckboxExample ์ปดํฌ๋„ŒํŠธ์— ๋„˜๊ฒจ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌ๋ Œ๋”๋ง ํ•œ๋‹ค.

์ฃผ์˜ํ•  ์ 

  • React ์ปดํฌ๋„ŒํŠธ๋Š” state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ƒˆ๋กญ๊ฒŒ ํ˜ธ์ถœ๋˜๊ณ , ๋ฆฌ๋ Œ๋”๋ง๋œ๋‹ค.
  • React state๋Š” ์ƒํƒœ ๋ณ€๊ฒฝ ํ•จ์ˆ˜setState() ํ˜ธ์ถœ๋กœ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค.

์ด ๊ธ€์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.
https://ko.reactjs.org/docs/getting-started.html

profile
๋ธ”๋กœ๊ทธ ์ด์ „ -> https://janechun.tistory.com
post-custom-banner

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