State

laluniaxยท2023๋…„ 11์›” 6์ผ
2

REACT

๋ชฉ๋ก ๋ณด๊ธฐ
8/20

State๋ž€?

  • ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” ๊ฐ’
    ๋งŒ์•ฝ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด์•ผ๋งŒ ํ•˜๋Š” ์ •๋ณด๋ผ๋ฉด state๋กœ ์ƒ์„ฑ !

State๋Š”useState()๋ฅผ ์‚ฌ์šฉํ•จ

useState๋Š” ๋ฆฌ์—‘ํŠธ์—์„œ ์ œ๊ณตํ•˜๋Š” Hook (๊ธฐ๋Šฅ)์ด๋‹ค.
Hook : ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜
๊ธฐ๋Šฅ --> ํ›…์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ

Only ๋ฆฌ์—‘ํŠธ์—์„œ๋งŒ ์กด์žฌํ•˜๋Š” ๊ฐœ๋…์ด์ž ๊ธฐ๋Šฅ(Hook)์ด๋‹ค.


useState ํ›… ์‚ฌ์šฉ

const [value, setValue] = useState (์ดˆ๊ธฐ๊ฐ’);
  • const ์„ ์–ธ ํ›„ ๋ฐฐ์—ด ์ƒ์„ฑ
  • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์ž๋ฆฌ์— state ์ด๋ฆ„,
  • ๋‘๋ฒˆ์งธ ์ž๋ฆฌ์— set + state ์ด๋ฆ„
  • useState() ์˜ ์ธ์ž์— state์˜ ์›ํ•˜๋Š” ์ดˆ๊ธฐ๊ฐ’ ๋„ฃ์œผ๋ฉด ๋

state ๋ณ€๊ฒฝ

import React, { useState } from "react";
function App() {
  const [name, setName] = useState("๊น€ํ• ์•„๋ฒ„์ง€");
  return (
    <div>
      {name}
      <button
        onClick={function () {
          setName("๋ฐ•ํ• ์•„๋ฒ„์ง€");
          // ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฐ•ํ• ์•„๋ฒ„์ง€๋กœ ๋ฐ”๋€Œ๊ฒŒ
        }}
      >
        ํด๋ฆญ
      </button>
    </div>
  );
}

export default App;

profile
grow constantly

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