React 'State' โš›๏ธ

React

๐ŸŒ€ What is React โ€˜Stateโ€™ ?

State === ์ƒํƒœ

์ธ๊ฐ„์—๊ฒŒ๋Š” ๋ฐฐ๊ณ ํ””, ์ ๋‹นํ•จ, ๋ฐฐ๋ถ€๋ฆ„ ์ด๋ผ๋Š” ์ƒํƒœ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์‚ฌ๋žŒ์€ ์ด๋ ‡๊ฒŒ 3๊ฐœ์˜ ์ƒํƒœ์— ๋”ฐ๋ผ์„œ ํ–‰๋™๋„ ๋‹ค๋ฅด๊ฒŒ ๋œ๋‹ค.

์ฆ‰ ๊ณ„์†ํ•ด์„œ ๋ณ€ํ™”ํ•˜๋Š” ํŠน์ • ์ƒํƒœ, ์ƒํƒœ์— ๋”ฐ๋ผ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋™์ž‘์„ ํ•จ

Statebutton

์œ„์˜ ์‚ฌ์ง„์€ ์š”์ฆ˜ ์œ ํ–‰ํ•˜๋Š” ๋‹คํฌํ…Œ๋งˆ์ด๋‹ค. ์ด๊ฒƒ๋„ Dark ์ด๊ฑฐ๋‚˜ Light๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ƒํƒœ๋ฅผ ์ง์ ‘ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“  ๊ธฐ๋Šฅ์ด๋‹ค.

๋ฆฌ์•กํŠธ์˜ ์ƒํƒœ๋Š” ์ปดํผ๋„ŒํŠธ๊ฐ€ ๊ฐ–๋Š” ํ…Œ๋งˆ์ฒ˜๋Ÿผ ๊ณ„์† ๊ฐ’์ด ๋ฐ”๋€” ๋™์ ์ธ ๋ฐ์ดํ„ฐ์ด๊ณ , ์ƒํƒœ๋ฅผ ๋ฐ”๊พธ๋Š” ๋“ฑ์˜ ๊ด€๋ฆฌ๋Š” ๊ทธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ์ปดํผ๋„ŒํŠธ๊ฐ€ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋Ÿผ ์ด์ œ ์˜ˆ์‹œ๋กœ ์•Œ์•„๋ณด๊ฒ ๋‹ค.

์—์‹œ

App Component

import React from "react";
import './App.css';
import MyHeader from "./MyHeader.jsx";
import Counter from "./Counter.jsx";
import Container from "./Container.jsx";

function App() {

  return (
    <Container>
      <div className="App">
        <MyHeader />
        <Counter Value={2}/>
        {/* ์ด๋ ‡๊ฒŒ ์†์„ฑ๊ฐ’์œผ๋กœ props๋ผ๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Œ */}
      </div>
    </Container>
  );
}

export default App;

Counter Component

/** @format */
import React, { useState } from 'react';
import OddEvenResult from './oddEvenResult';

const Counter = () => {
  const [count, setCount] = useState(counterValue3); // props ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ํ”„๋กœํผํ‹ฐ๋กœ ์ „๋‹ฌ ๋ฐฉ๋ฒ•

  const onIncrease = () => {
    setCount(count + 1);
  };

  const onDecrease = () => {
    setCount(count - 1);
  };

  return (
    <div>
      <h2>{count}</h2>
      <button onClick={onIncrease}>+</button>
      <button onClick={onDecrease}>-</button>
      <br />
      <OddEvenResult className='result' count={count} />
    </div>
  );
};

Counter.defaultProps = {
  counterValue3: 0,
};

export default Counter;

์œ„์˜ ์ฝ”๋“œ๋Š” ์ˆซ์ž ๋ฐ์ดํ„ฐ์ธ count์„ +์™€ -์˜ ๋ฒ„ํŠผ์œผ๋กœ 1์”ฉ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๊ฑฐ๋‚˜ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๊ฐ„๋‹จํ•˜์ง€๋งŒ ์ด๋ณด๋‹ค ๋” ์ข‹์€ State ์ฆ‰ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ˆ์‹œ๋Š” ์—†๋‹ค. ์ด count๋Š” Counter component์˜ ํ•˜์œ„ component๋“ค์—๊ฒŒ Props์œผ๋กœ count ๊ฐ’์„ ์ค„ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ์ด ๊ฐ’์€ ๊ฐ component ๋ผ๋ฆฌ ์œ ๊ธฐ์ ์œผ๋กœ ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•˜๊ฒŒ ๋œ๋‹ค.

โ›”๏ธ ๋‹ค๋งŒ!! ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ๊ณ„์† ๋‹ค๋ฅธ ๊ฐ’์„ ๋„ฃ์–ด ์‚ฌ์šฉํ–ˆ๋”๋ผ๋ฉด ๋ฆฌ์•กํŠธ๋Š” count์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” setCount๋กœ๋งŒ ์ ‘๊ทผ์„ ํ•ด์•ผํ•œ๋‹ค. ์ž๋ฐ”๋‚˜ ๋‹ค๋ฅธ ์–ธ์–ด๋“ค์˜ getter, setter ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์œผ๋กœ ๋ณด๋ฉด ๋ ๊ฑฐ ๊ฐ™๋‹ค.

๐Ÿ†˜ Props์™€ State์˜ ์ฐจ์ด

ReactvsProps

Props(Properties)

  1. Arguments of function
  2. ์ปดํฌ๋„ŒํŠธ ๋ฐ–์— ์žˆ๋‹ค.
  3. ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋Š” ์•ฑ์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ–ˆ์„๋•Œ initial value๊ฐ™์€ ๊ฒƒ์„ props๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.
  4. ์ปดํฌ๋„ŒํŠธ ๋ฐ–์—์„œ handle ํ•œ๋‹ค.
  5. ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  6. ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€์•Š๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ๊ฒƒ์€ props๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  7. Staticํ•œ ๊ฒƒ๋„ props๋กœ ๋งŒ๋“ค๋ฉด ์ข‹๋‹ค

State

  1. ์ปดํฌ๋„ŒํŠธ ์•ˆ์— ์žˆ๋‹ค.
  2. ์นด์šดํŠธ ์•ฑ์„ ๋งŒ๋“ค ๋•Œ state์•ˆ์— ๋ณ€๊ฒฝ๋˜๋Š” ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  3. ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ handle ํ•œ๋‹ค.
  4. State๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.
  5. ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์–ด์•ผ ๋˜๋Š” ๊ฑด state๋กœ ์ €์žฅํ•ด์•ผ ๋œ๋‹ค.
  6. ์œ ์ €๊ฐ€ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” Form๊ฐ™์€ ๊ฒƒ์— ์“ฐ๋ฉด ์ข‹๋‹ค.