๐Ÿ™ State

๊น€์ฒ ์ค€ยท2022๋…„ 1์›” 17์ผ
0

REACT

๋ชฉ๋ก ๋ณด๊ธฐ
7/38
post-custom-banner

๐Ÿ” State

state๋Š” ์ปดํฌ๋„ŒํŠธ๋‚ด์—์„œ ์–ด๋– ํ•œ ๊ฐ’์„ ๋™์ ์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
์ฆ‰, state๋Š” ๋™์ ์œผ๋กœ ๊ฐฑ์‹ ๋˜๋Š” ๊ฐ’์ด๋‹ค.

  • ์•ž์„œ ๋ณด์•˜๋“ฏ์ด props๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
    ๋”ฐ๋ผ์„œ props ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ state๊ฐ€ ์žˆ๋‹ค.

  • ๊ผญ props ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ๋™์ ์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด state๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

state์˜ ํŠน์ง•

  1. state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” setState๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
    setState๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  state๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๋ฉด ์—…๋ฐ์ดํŠธ๊ฐ€ ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค.

  2. setState๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
    ์ฆ‰, setState๋ฅผ ์‹คํ–‰ํ•œ๋’ค์— ์ฆ‰์‹œ state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ผ๋ถ€ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๋’ค์— state๊ฐ€ ์—…๋ฐ์ดํŠธ๋  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  3. state๊ฐ’์€ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๋ฉฐ ์—…๋ฐ์ดํŠธ๋˜์•ผํ•œ๋‹ค.
    ์ตœ์ ํ™”ํ•˜๋Š”๋ฐ ์šฉ์ดํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

shouldComponentUpdate๋‚˜ React.memo๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ์ƒํƒœ๊ฐ’๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ณ€ํ™”๊ฐ€ ์—†์œผ๋ฉด ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

state๊ฐ€ ๋ถˆ๋ณ€์„ฑ์ด ์œ ์ง€ํ•˜์ง€์•Š๊ณ  ์ด์ „๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚จ๋‹ค๋ฉด ์ด์ „ state์™€ ๋ณ€๊ฒฝ๋˜๋Š” state๊ฐ€ ๊ฐ™์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ๊ฐ™์€ ์ตœ์ ํ™” ์ž‘์—…์„ ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

๋˜ํ•œ react๋Š” virtual DOM์„ ํ†ตํ•ด ๋ณ€๊ฒฝ๋˜๋Š” DOM๋งŒ ํšจ์œจ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ด์ฃผ๋Š”๋ฐ ์ด์ „์ƒํƒœ๊ฐ’๋„ ๋ฐ”๋€Œ๋Š” ์ƒํƒœ๊ฐ’๊ณผ ๊ฐ™๊ฒŒ ๋งŒ๋“ค๋ฉด DOM์ด ์—…๋ฐ์ดํŠธ๋˜์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค.

๋ง๋ถ™ํ˜€ ๋ฆฌ์•กํŠธ๋Š” ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฆ‰, side effect๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š” ๊ฒƒ์„ ์ถ”๊ตฌํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋‚ด๋ถ€์—์„œ ์™ธ๋ถ€๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์„ ์ง€์–‘ํ•œ๋‹ค.

๋‹ค์Œ์€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์˜ state ์‚ฌ์šฉ ์˜ˆ์‹œ์ด๋‹ค.


๐Ÿงฉ state in Class Component

import React, { Component } from "react";

class ClassComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      number: 0,
    };
  }
  render() {
    const { number } = this.state;
    return (
      <div>
        <span>{number}</span>
        <button
          onClick={() => {
            this.setState({ number: number + 1 });
          }}
        >
          Increase
        </button>
      </div>
    );
  }
}

export default ClassComponent;

  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” constructor ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋”ฐ๋ผ์„œ constructor()๋ฉ”์„œ๋“œ ๋‚ด์—์„œ state๊ฐ’์„ ๊ฐ์ฒด ํƒ€์ž…์œผ๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

  • state ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ณ ์ž ํ•  ๋•Œ์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ ์˜ˆ์‹œ๋กœ๋Š”
    ๋ฆฌ์•กํŠธ ์š”์†Œ(button)์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜์—ฌ setState() ๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜๋กœ ๊ฐ์ฒด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

  • ์ฐธ๊ณ ๋กœ setState๋Š” render()๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  • React.Component๋ฅผ ์ƒ์†ํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” props๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ super๋กœ props๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.
    (super class์˜ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” sub class๋Š” super()๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜๋กœ ๋ฐ›์•„์ค˜์•ผํ•จ.)
  • ํ•˜์ง€๋งŒ constructor()๋ฉ”์„œ๋“œ๋ฅผ ์ƒ๋žตํ•˜์—ฌ๋„ React.Component๋กœ๋ถ€ํ„ฐ props๋ฅผ ์•”๋ฌต์ ์œผ๋กœ ์ƒ์†๋ฐ›์•„ props๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์œ„๋ณด๋‹ค ํŽธํ•˜๊ฒŒ state๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

import React, { Component } from "react";

class ClassComponent extends Component {
  state = {
    number: 0,
  };
  render() {
    const { number } = this.state;
    return (
      <div>
        <span>{number}</span>
        <button
          onClick={() => {
            this.setState({ number: number + 1 });
          }}
        >
          Increase
        </button>
      </div>
    );
  }
}

export default ClassComponent;

constructor() ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ๋žตํ•˜์—ฌ ํŽธ๋ฆฌํ•˜๊ฒŒ state๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿงฉ state in Function Component

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ state๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

import React, { useState } from "react";

const FunctionComponent = () => {
  const chooseColor = ["red", "orange", "yellow", "green", "blue", "purple"];
  const [color, setColor] = useState("black");
  function colorChange() {
    const ran = Math.floor(Math.random() * 6);
    setColor(chooseColor[ran]);
  }
  return (
    <>
      <div
        style={{
          width: "100px",
          height: "100px",
          backgroundColor: color,
        }}
      ></div>
      <button onClick={colorChange}>Change</button>
    </>
  );
};

export default FunctionComponent;

๐Ÿ‘‰ useState

  • useState๋ผ๋Š” ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ state๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.
  • useState๋Š” React์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
import {useState} from "react"
  • useState()๋Š” ๋‘ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค

    useState() => [์ง€์ •๊ฐ’ , ์ง€์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜]

  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐฐ์—ด ๊ตฌ์กฐ๋ถ„ํ•ด๋ฅผ ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
  const [color, setColor] = useState("black");

color : ์ง€์ •๊ฐ’
setColor : color๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

์ด์ฒ˜๋Ÿผ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋ณด๋‹ค ๊ฐ„ํŽธํ•˜๊ฒŒ state๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.


๐Ÿ”‘ change props using state

  • props๋Š” ์œ„์ฒ˜๋Ÿผ ์ปดํฌ๋„ŒํŠธ๋‚ด์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ• ๋•Œ์—๋„ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ
    ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state ๊ฐ’์„ ์ž์‹์ปดํฌ๋„ŒํŠธ์˜ props๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์ด๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ state์™€ props๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

ParentComponent.js

import React, { Component } from "react";
import ChildComponent from "./ChildComponent";
class ParentComponent extends Component {
  state = {
    number: 0,
  };
  render() {
    const { number } = this.state;
    const changeNumber = () => {
      this.setState({ number: number + 1 });
    };
    return (
      <div>
        <ChildComponent id={number} changeNumber={changeNumber} />
      </div>
    );
  }
}

export default ParentComponent;

ChildCoponent.js

import React from "react";

const ChildComponent = ({ id, changeNumber }) => {
  return (
    <div>
      <span>{id}</span>
      <button onClick={changeNumber}>Increase</button>
    </div>
  );
};

export default ChildComponent;
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ state๊ฐ’(number)๊ณผ ๊ฐฑ์‹  ํ•จ์ˆ˜(changeNumber)๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ props๋กœ ์ „๋‹ฌํ•ด์ฃผ์–ด ์ž์‹์ปดํฌ๋„ŒํŠธ์˜ props๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ด์ฒ˜๋Ÿผ props ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•˜๋ฉด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ state๊ฐ’์„ ์ •์˜ํ•˜์—ฌ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.

์ด๊ฒƒ์ด ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์˜ ์ผ๋ฐ˜์ ์ธ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋‹ค.
๐Ÿ‘๐Ÿ‘

Reference

post-custom-banner

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