[React] state

๊ฐ•์€๋น„ยท2022๋…„ 1์›” 11์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
5/36
post-custom-banner

react ์Šคํ„ฐ๋””์—์„œ ๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์ด๋ผ๋Š” ์ฑ…์„ ์„ ์ •ํ–ˆ๊ณ  ์ด ์ฑ…์„ ์ฝ๊ณ  ๋ฐฐ์šด ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค.


๐Ÿ“Œ state๋ž€?

  • ์ปดํฌ๋„ŒํŠธ ์ž์ฒด์ ์œผ๋กœ ์ง€๋‹Œ ๊ฐ’์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

props vs state

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

๐Ÿ“™ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ state

import { Component } from "react";

class Counter extends Component {
    constructor(props) {
       super(props);
       // state์˜ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
       // ๊ฐ์ฒด ํ˜•์‹!
       this.state = {
          number: 0
       };
    }
    render() {
        // state๋ฅผ ์กฐํšŒํ•  ๋•Œ๋Š” this.state๋กœ ์กฐํšŒ
        const { number } = this.state;
        return (
            <div>
                <h1>{number}</h1>
                <button 
                    onClick = { () => this.setState({number: number + 1})}
                >
                    +1
                </button>
            </div>
        );
    }
}

export default Counter;
  • props๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ (constructor ๋ฉ”์„œ๋“œ) ๋‚ด๋ถ€์—์„œ super(props)๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
  • super(props)๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Component ํด๋ž˜์Šค๊ฐ€ ์ง€๋‹Œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์ค€๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ์˜ state๋Š” ๊ฐ์ฒด ํ˜•์‹์ด์–ด์•ผ ํ•œ๋‹ค.
  • render ๋ฉ”์„œ๋“œ์—์„œ state๋ฅผ ์กฐํšŒํ•  ๋•Œ๋Š” this.state ์ด์šฉ

โœจ state ๊ฐ์ฒด ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐ’์ด ์žˆ์„ ๋•Œ

import { Component } from "react";

class Counter extends Component {
    // state์˜ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
    // ๊ฐ์ฒด ํ˜•์‹!
    state = {
        number: 0,
        fixedNumber: 0
    };
    constructor(props) {
       super(props);
    }
    render() {
        // state๋ฅผ ์กฐํšŒํ•  ๋•Œ๋Š” this.state๋กœ ์กฐํšŒ
        const { number, fixedNumber } = this.state;
        return (
            <div>
                <h1>{number}</h1>
                <h2>๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๊ฐ’: {fixedNumber}</h2>
                <button 
                    onClick = { () => this.setState({number: number + 1})}
                >
                    +1
                </button>
            </div>
        );
    }
}

export default Counter;

โœจ this.setState๊ฐ€ ๋๋‚œ ํ›„ ํŠน์ • ์ž‘์—… ์‹คํ–‰ํ•˜๊ธฐ

  • setState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๋‚œ ๋‹ค์Œ์— ํŠน์ • ์ž‘์—…์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” setState์˜ ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.

const onClick = () => {
	this.setState({
        number: number + 1
    }, () => {
        console.log("setState ํ˜ธ์ถœ");
    })}
};
<button onClick={onClick}>
    +1
</button>

โœจ setState์— ๊ฐ์ฒด ๋Œ€์‹  ํ•จ์ˆ˜ ์ธ์ž ์ „๋‹ฌํ•˜๊ธฐ

๐Ÿ’ก State Updates May Be Asynchronous
React may batch multiple setState() calls into a single update for performance.
๐Ÿ’ก State Updates are Merged
When you call setState(), React merges the object you provide into the current state.

  • setState()๋Š” ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
  • state๋Š” ๊ฐ์ฒด์ด๋‹ค.
  • setState๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์ „๋‹ฌ ๋ฐ›์€ ๊ฐ state(๊ฐ์ฒด)๋ฅผ ํ•ฉ์น˜๊ณ (merging) ํ•ฉ์ณ์ง„ ๋‹จ์ผ ๊ฐ์ฒด์— ๋Œ€ํ•ด setState()๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • merge ๊ณผ์ •์—์„œ ๊ฐ™์€ key๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ๊ฐ’์„ ๋ฎ์–ด์“ด๋‹ค.
  • ์ฆ‰, setState()๋ฅผ ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด Batch ์ฒ˜๋ฆฌ(์ผ๊ด„ ์ฒ˜๋ฆฌ)๋ฅผ ํ•œ๋‹ค.
  • ์›น ํŽ˜์ด์ง€ ๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง ํšŸ์ˆ˜๋ฅผ ์ค„์—ฌ ์ข€ ๋” ๋น ๋ฅธ ์†๋„๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.
  • ์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด setState์— ๊ฐ์ฒด ๋Œ€์‹  ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
  • ์ธ์ž๋กœ ์ „๋‹ฌํ•œ ํ•จ์ˆ˜๋Š” ์ด์ „ ์ƒํƒœ (prevState)๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๊ณ  ์—…๋ฐ์ดํŠธ๊ฐ€ ์ ์šฉ๋œ ์‹œ์ ์˜ props๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.

๐Ÿ“™ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ useState ์‚ฌ์šฉํ•˜๊ธฐ

  • v16.8 ์ดํ›„๋ถ€ํ„ฐ useState ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ state๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • useState ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐฐ์—ด์ด ๋ฐ˜ํ™˜๋˜๋Š”๋ฐ, ์ฒซ๋ฒˆ์งธ ์›์†Œ๋Š” ํ˜„์žฌ ์ƒํƒœ๊ฐ’์ด๊ณ , ๋‘๋ฒˆ์งธ ์›์†Œ๋Š” ์ƒํƒœ๋ฅผ ๋ฐ”๊พธ์–ด ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. (setter ํ•จ์ˆ˜)
  • useState ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ’์„ ์ธ์ž๋กœ ๋ณด๋‚ธ๋‹ค.
import React, { useState } from 'react';

function Counter() {
    // ๋ฐฐ์—ด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น
    const [number, setNumber] = useState(0);
    
    const onIncrease = () => {
        setNumber(number + 1);
    };
    
    const onDecrease = () => {
        setNumber(number - 1);
    };
    
    return (
        <div>
            <h1>{number}</h1>
            <button onClick = {onIncrease}>+1</button>
            <button onClick = {onDecrease}>-1</button>
        </div>
    );

}
export default Counter;

โœจ ํ•จ์ˆ˜ํ˜• ์—…๋ฐ์ดํŠธ

  • ๋งŒ์•ฝ ์ด์ „์˜ ์ƒํƒœ๊ฐ’์„ ์ฐธ๊ณ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, setState์— ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.
  • ์ด ํ•จ์ˆ˜ ์ธ์ž๋Š” ์ „์˜ ์ƒํƒœ๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›๊ณ  ์—…๋ฐ์ดํŠธ๋œ ์ƒํƒœ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// ํ•จ์ˆ˜ํ˜• ์—…๋ฐ์ดํŠธ
const onIncrease = () => {
    setNumber(prevNumber => prevNumber + 1);
  }

  const onDecrease = () => {
    setNumber(prevNumber => prevNumber - 1);
  }

โœจ state ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

  • state ๊ฐ’์„ ๋ฐ”๊พธ์–ด์•ผ ํ•  ๋•Œ๋Š” setState ํ˜น์€ useState๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋ฐ›์€ setter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•œ๋‹ค.
// ์ž˜๋ชป๋œ ์ฝ”๋“œ
this.state.number = this.state.number + 1;
this.state.array = this.state.array.push(2);
this.state.object.value = 5;

const [object, setObject] = useState({a: 1, b: 1});
object.b = 2;
  • ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•  ๋•Œ๋Š” ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด ์‚ฌ๋ณธ์„ ๋งŒ๋“ค๊ณ  ๊ทธ ์‚ฌ๋ณธ์— ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ ํ›„ ๊ทธ ์‚ฌ๋ณธ์˜ ์ƒํƒœ๋ฅผ setState ํ˜น์€ setter ํ•จ์ˆ˜ํ†ตํ•ด ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

์ฐธ๊ณ : React ๊ณต์‹ ๋ฌธ์„œ

post-custom-banner

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