state, useState

Ceciliaยท2023๋…„ 1์›” 12์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
6/9
post-thumbnail

https://www.youtube.com/watch?v=p5ZP4xzhRxk&list=PLZKTXPmaJk8J_fHAzPLH8CJ_HO_M33e7-&index=7




๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ์ƒํƒœ๊ด€๋ฆฌ์™€ state์˜ ๊ฐœ๋…

export default function Hello() {
  let name = "Mike";

  function changeName() {
    name = name === "Mike" ? "Jane" : "Mike";
    //name์ด Mike์ผ๋•Œ๋Š” Jane์œผ๋กœ, Jane์ผ๋•Œ๋Š” Mike๋กœ ๋ฐ”๊ฟ”์คŒ
    console.log(name);
  }

  return (
    <div>
      <h1>state</h1>
      <h2>{name}</h2>
      <button onClick={changeName}>Change</button>
    </div>
  );
}

์œ„์˜ ์ฝ”๋“œ๋Œ€๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋ฒ„ํŠผ์„ ์•„๋ฌด๋ฆฌ ํด๋ฆญํ•ด๋„ ํ™”๋ฉด์— ์—…๋ฐ์ดํŠธ ๋˜์ง€ ์•Š๋Š”๋‹ค.
๊ทธ๋ž˜์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

export default function Hello() {
  let name = "Mike";

  function changeName() {
    name = name === "Mike" ? "Jane" : "Mike";
    //name์ด Mike์ผ๋•Œ๋Š” Jane์œผ๋กœ, Jane์ผ๋•Œ๋Š” Mike๋กœ ๋ฐ”๊ฟ”์คŒ
    console.log(name);
    document.getElementById("name").innerText = name; //๐Ÿ‘ˆ ์ถ”๊ฐ€
  }

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">{name}</h2>   //๐Ÿ‘ˆ id ์ถ”๊ฐ€
      <button onClick={changeName}>Change</button>
    </div>
  );
}

let = name์€ ๋ณ€์ˆ˜์ผ ๋ฟ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ด€๋ฆฌํ•˜๊ณ ์žˆ๋Š” ์ƒํƒœ๊ฐ’, state๊ฐ€ ์•„๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋„ React์—์„œ๋Š” ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  UI๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒํ•ด์•ผ ํ• ๊นŒ?๐Ÿค”



์ƒํƒœ๊ฐ’ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ useState ์‚ฌ์šฉ


useState๋Š” React Hook์ด๋‹ค.
React Hook ์ข…๋ฅ˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ, useState๋Š” ๊ทธ ์ค‘ ํ•˜๋‚˜๋‹ค.


๐Ÿ” ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

1) import

import { useState } from "react";


2) useState() ์‚ฌ์šฉ ์ฝ”๋“œ ์ž‘์„ฑ

const [name, setName] = useState();

useState๋Š” ๋ฐฐ์—ด([name, setName])์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋กœ,
๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’ [name]์€ state์ด๋ฉฐ(๋ณ€์ˆ˜๋ช…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค)
๋‘ ๋ฒˆ์งธ ๊ฐ’ [setName]์€ state๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋‹ค.

๋‘ ๋ฒˆ์งธ ๊ฐ’ [setName]ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์ด ๋ฐ”๋€Œ๋ฉด React๋Š” ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ๋ Œ๋”๋ง ํ•ด์ค€๋‹ค.


3) useState() ๊ด„ํ˜ธ ์•ˆ์— ์ดˆ๊ธฐ ๊ฐ’ ์ž‘์„ฑ

const [name, setName] = useState("์ดˆ๊ธฐ๊ฐ’");




1)~3)๋ฒˆ๊นŒ์ง€ ์ ์šฉํ•œ ์ฝ”๋“œ

import { useState } from "react";

export default function Hello() {
  const [name, setName] = useState("Mike");

  function changeName() {
    const newName = name === "Mike" ? "Jane" : "Mike";
    //name์ด Mike์ผ๋•Œ๋Š” Jane์œผ๋กœ, Jane์ผ๋•Œ๋Š” Mike๋กœ ๋ฐ”๊ฟ”์คŒ

    setName(newName);
  }

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">{name}<;/h2>
      <button onClick={changeName}>Change</button>
    </div>
  );
}

๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ

import { useState } from "react";

export default function Hello() {
  const [name, setName] = useState("Mike");

  function changeName() {
    setName(name === "Mike" ? "Jane" : "Mike"); //๐Ÿ‘ˆ ๋ณ€๊ฒฝ
  }

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">{name}</h2>
      <button onClick={changeName}>Change</button>
    </div>
  );
}

๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ

import { useState } from "react";

export default function Hello() {
  const [name, setName] = useState("Mike");

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">{name}<;/h2>
      <button
        onClick={() => {
          setName(name === "Mike" ? "Jane" : "Mike"); //๐Ÿ‘ˆ ๋ณ€๊ฒฝ
        }}
      >
        Change
      </button>
    </div>
  );
}


useState๋ฅผ ์‚ฌ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ์˜ ํ™œ์šฉ๊ณผ ์ดํ•ด


์œ„์—์„œ ์ž‘์„ฑํ•œ Hello.js ์ปดํฌ๋„ŒํŠธ๋ฅผ App.js์—์„œ ์„ธ ๋ฒˆ ์ž‘์„ฑํ•˜๋ฉด?

import "./App.css";
import Hello from "./component/Hello";
import Welcome from "./component/Welcome";

function App() {
  return (
    <div className="App">
      <Hello />  //๐Ÿ‘ˆ Hello ์ปดํฌ๋„ŒํŠธ ์„ธ ๋ฒˆ
      <Hello />  //๐Ÿ‘ˆ
      <Hello />  //๐Ÿ‘ˆ
    </div>
  );
}

export default App;

ํ™”๋ฉด์—๋„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์ ์€
Change ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋„ Mike, Jane = state๊ฐ’ ์ „์ฒด๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด๋‹ค.


function App() {
  return (
    <div className="App">
      <Hello />  //๐Ÿ‘ˆ ์ฒซ์งธ
      <Hello />  //๐Ÿ‘ˆ ๋‘˜์งธ
      <Hello />  //๐Ÿ‘ˆ ์…‹์งธ
    </div>
  );
}

ํŽธ์˜์ƒ ์ฒซ์งธ~์…‹์งธ<Hello/>๋ผ๊ณ  ๋ถˆ๋Ÿฌ์„œ ์„ค๋ช…ํ•˜์ž๋ฉด

์ฒซ์งธ<Hello/>์ธ ๋งจ ์œ„ Change๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ฒซ์งธ<Hello/>์˜ name๋งŒ ๋ฐ”๋€๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‘˜์งธ<Hello/>์ธ ์ค‘๊ฐ„ Change๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋„ ๋‘˜์งธ<Hello/>์ธ ์ค‘๊ฐ„ name๋งŒ ๋ฐ”๋€๋‹ค.


์ด๋ ‡๊ฒŒ ๋”ฐ๋กœ๋”ฐ๋กœ ์ƒํƒœ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ !

profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

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