MapleStory Finger Point

๐Ÿ”ต React

[React] state

HYEJU01 2024. 8. 28. 20:26

1. State

๐Ÿ˜บ  state ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ ?? 

  1. ๋ฆฌ์•กํŠธ์—์„œ state๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค
  2. state๊ฐ€ ์ฒด์ธ์ง€ ๋˜๋ฉด ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•˜์—ฌ ๋ฆฌ๋ Œ๋”๋ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  3. ํด๋ž˜์Šค vs ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
  4. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ํ›… Hook๊ฐœ๋…์„ ์ด์šฉํ•ด์„œ ๋”์šฑ ์‰ฝ๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค.

 

2.  useState - hook

  useState(์ดˆ๊ธฐ๊ฐ’) - ํ•จ์ˆ˜(hook)

 

๐Ÿ“  useState() : ๋ฐฐ์—ด๋ฐ˜ํ™˜

 

์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ์š”์†Œ์—๋Š” ํ˜„์žฌ๊ฐ’์„, ๋‘๋ฒˆ์งธ ์š”์†Œ๋Š” ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” (setter) ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

const [data, setData] = useState('์ดˆ๊ธฐ๊ฐ’')

 

state๋Š” ์–ด๋–ค ํƒ€์ž…์ด๋˜ ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค. (str, number, object, array, boolean… )

state๋Š” ์—ฌ๋Ÿฌ๊ฐœ ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๐Ÿ˜บ  ์ด๋•Œ !! state ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ผญ ์ง€ํ‚ค๋„๋ก ํ•˜์ž !

 

1. ์ƒํƒœ ์ง์ ‘ ์ˆ˜์ • ๊ธˆ์ง€: State๋Š” ์ง์ ‘ ์ˆ˜์ •ํ•˜๋ฉด ์•ˆ๋œ๋‹ค

(๋ฆฌ์•กํŠธ๊ฐ€ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๊ฐ์ง€ํ•˜์ง€ ๋ชปํ•จ , ์ฆ‰ ๋ Œ๋”๋ง ์‚ฌ์ดํด์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ) 

 

2. ์ƒํƒœ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜ ์‚ฌ์šฉ  : setter ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

setState ํ•จ์ˆ˜(ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ)

์ƒํƒœ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜(ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ, ์˜ˆ: setSomeValue)๋ฅผ ์‚ฌ์šฉ

 

 

๐Ÿ“ ์ž˜๋ชป๋œ ์ฝ”๋“œ

const StateComponent = () => {
    const [msg, setData] = useState('์ดˆ๊ธฐ๊ฐ’')
		const exter = () => {
				msg = '๋‚˜๋Š” ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ€๊บผ์•ผ!'; //error!!!!	(๋ Œ๋”๋ง x)	
		}
		return (
        <div>
            <h3>{msg}</h3>
            <button onClick={enter}>์ž…์žฅ</button>
        </div>
    )
}

 

 

๐Ÿ“ ์˜ฌ๋ฐ”๋ฅธ ๋ณ€๊ฒฝ

const StateComponent = () => {
    const [msg, setData] = useState('์ดˆ๊ธฐ๊ฐ’')

    const enter = () => setData('์ž…์žฅํ–ˆ์Šต๋‹ˆ๋‹ค')
    const exit = () => setData('ํ‡ด์žฅํ–ˆ์Šต๋‹ˆ๋‹ค')

    return (
        <div>
            <h3>{msg}</h3>
            <button onClick={enter}>์ž…์žฅ</button>
            <button onClick={exit}>ํ‡ด์žฅ</button>
        </div>
    )
}

 

 

 

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

๋”๋ณด๊ธฐ
import React from 'react';

class MyComponent3 extends React.Component {
		//์ƒ์„ฑ์ž์—์„œ props ๋ฐ›๊ธฐ
    constructor(props) {
        super(props);
        this.state = {
            a : 1,
            b : props.age
        }
    }
  
    //ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” render() ์•ˆ์— ๋ฆฌํ„ด์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
    render() {
        //props๋ฐ›๊ธฐ
        let myName = "์ด์ˆœ์‹ "; //ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ์€ render์•ˆ์—์„œ ์‚ฌ์šฉํ•จ
        let {name, age} = this.props; //ํด๋ž˜์Šค์—์„œ props์˜ ์‚ฌ์šฉ

        return (
            <div>
                {myName}์˜ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ<br/>
                ํ”„๋กœํผํ‹ฐ1:{name}<br/>
                ํ”„๋กœํผํ‹ฐ2:{age}<br/>
                state์˜์‚ฌ์šฉ1: {this.state.a}<br/>
                state์˜์‚ฌ์šฉ2: {this.state.b}<br/>

            </div>
        )
    }
    
}
export default MyComponent3;

 

 

 

 

 

๐Ÿ˜บ  ์ž˜๋ชป๋œ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜๋ฉด ๋ฆฌ๋ Œ๋”๋ง์˜ ์š”์ธ์„ ํ™•์ธํ•ด์•ผํ•œ๋‹ค!

 

๐Ÿ“   ๋ฆฌ๋ Œ๋”๋ง์„ ์•ผ๊ธฐํ•˜๋Š” ์š”์ธ

  1. setState์— ์˜ํ•œ state ๋ณ€๊ฒฝ
  2. ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌ๋ฐ›์€ Props์˜ ๋ณ€๊ฒฝ
  3. ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง ⇒ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง
  4. Provider์˜ value prop๊ฐ€ ๋ฐ”๋€” ๋•Œ, Context.Provider ํ•˜์œ„์—์„œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง (ContextAPI ๊ณต๋ถ€ํ•„์š”)
  5. ๋ฆฌ๋•์Šค ์Šคํ† ์–ด์˜ state์— ๋ณ€๊ฒฝ ๋ฐœ์ƒ ์‹œ, useSelector๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฆฌ๋•์Šค ์Šคํ† ์–ด์˜ state๋ฅผ ์กฐํšŒํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง (๋ฆฌ๋•์Šค ๊ณต๋ถ€ํ•„์š”)

 

 

 


๐Ÿ“ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์˜ˆ์ œ


[ App.js ]

import { Fragment } from "react";
import StateComponent from "./component/StateComponent.js";
import CountComponent from "./component/CountComponent.js";
import ClassComponent from "./component/ClassComponent.js";

function App() {


  return (
    <Fragment>
      <StateComponent />
      <CountComponent />
      <ClassComponent name="๊ธธ๋™์ž‰" age={20}/>
    </Fragment>
  )

}

export default App;

 

 

[StateComponent.js]

import { useState } from "react";


function StateComponent() {

  //useState๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” hook
  //๋ฐ˜ํ™˜์ด ๋ฐฐ์—ด์ด๊ณ  [0]๋ฒˆ์งธ์— ํ˜„์žฌ๋ฐ์ดํ„ฐ, [1]๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” setterํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•จ
  let [data, setData] = useState('์ดˆ๊ธฐ๊ฐ’');
  let [msg, setMsg] = useState('black');
  
  // let data = result[0]; //data
  // let setData = result[1]; //fun

  let enter = () => {
    //data = "์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด?"; //๊ฐ’์„ ์ง์ ‘ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.
    setData("์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฑฐ์ž„"); //๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” setterํ•จ์ˆ˜ -> ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ change, ๋ฆฌ๋ Œ๋”๋ง ์ˆ˜ํ–‰
  }

  return (
    <div>
      ํ˜„์žฌ ๋ฐ์ดํ„ฐ {data}
      <br/>
      ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ <button onClick={enter}>์ž…์žฅ</button>
      <br/>
      ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ <button onClick={() => setData("ํ‡ด์žฅํ–ˆ์Šต๋‹ˆ๋‹ค") }>ํ‡ด์žฅ</button>
      <br/>

      <h3 style={{color: msg}}>{msg}</h3>
      <button onClick={ () => setMsg('red') }>๋ถ‰์€์ƒ‰</button>
      <button onClick={ () => setMsg('green') }>ํ‘ธ๋ฅธ์ƒ‰</button>
      <button onClick={ () => setMsg('yellow') }>๊ทธ ์‚ฌ์ด 3์ดˆ ์งง์€์ƒ‰</button>


    </div>
  )

}

export default StateComponent;

 

  //useState๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” hook
  //๋ฐ˜ํ™˜์ด ๋ฐฐ์—ด์ด๊ณ  [0]๋ฒˆ์งธ์— ํ˜„์žฌ๋ฐ์ดํ„ฐ, [1]๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” setterํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•จ
  let [data, setData] = useState('์ดˆ๊ธฐ๊ฐ’');
  let [msg, setMsg] = useState('black');

 

let data = result[0]; //data
let setData = result[1]; //func

 

 ํ˜„์žฌ ๋ฐ์ดํ„ฐ {data}

 

  • ๋ฐ์ดํ„ฐ ์ถœ๋ ฅํ•˜๊ธฐ

 

 

 

let enter = () => {
    //data = "์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด?"; //๊ฐ’์„ ์ง์ ‘ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.
    setData("์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฑฐ์ž„"); //๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” setterํ•จ์ˆ˜ -> ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ change, ๋ฆฌ๋ Œ๋”๋ง ์ˆ˜ํ–‰
  }
  • ๊ฐ’์„ ์ง์ ‘ ๋ฐ”๊พธ๋ฉด ์•ˆ๋˜๊ณ 
  • ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” setter ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์นœ๋‹ค.

 

 ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ <button onClick={enter}>์ž…์žฅ</button>
  <br/>
  ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ <button onClick={() => setData("ํ‡ด์žฅํ–ˆ์Šต๋‹ˆ๋‹ค") }>ํ‡ด์žฅ</button>
  <br/>

 

 

  • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด enter ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ data ๊ฐ’์ด ๋ฐ”๋€๋‹ค.

 

 

  • ์ธ๋ผ์ธ ํ•จ์ˆ˜๋กœ ๋ฐ”๋กœ ์ต๋ช…ํ•จ์ˆ˜ ์ž‘์„ฑํ•ด์„œ setData("๋ณ€๊ฒฝ๊ฐ’") ์œผ๋กœ data ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

 

 

      <h3 style={{color: msg}}>{msg}</h3>
      <button onClick={ () => setMsg('red') }>๋ถ‰์€์ƒ‰</button>
      <button onClick={ () => setMsg('green') }>ํ‘ธ๋ฅธ์ƒ‰</button>
      <button onClick={ () => setMsg('yellow') }>๊ทธ ์‚ฌ์ด 3์ดˆ ์งง์€์ƒ‰</button>

 

 

  let [msg, setMsg] = useState('black');
  • ์ƒ‰์ƒ ๋ณ€๊ฒฝ์„ ์œ„ํ•œ ํ›…

 

๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด setMsg() ๊ฐ€ ๋™์ž‘ํ•˜๋ฉด์„œ

<h3>์˜ color ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚จ๋‹ค.

 

 


 


[CountComponent.js]

 

import { useState } from "react";

function CountComponent() {

  let [cnt, setCount] = useState(0);

//setData(ํ•จ์ˆ˜); 
// setData((prev) => {
//   return prev + 1;  
// }) 


  return (
    <div>
      <h3> {cnt} </h3>
      <button onClick= { ()=> setCount(cnt + 1)}> ์ฆ๊ฐ€ </button>
      <button onClick= { ()=> setCount(cnt - 1)}> ๊ฐ์†Œ </button>
      <button onClick= { ()=> setCount(0)}> ์ดˆ๊ธฐํ™” </button>
    </div>
  )
}

export default CountComponent;

 

 


 

๐Ÿ“  ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ [ClassComponent.js]

import { Component } from "react";

class ClassComponent extends Component {
  //useState ์‚ฌ์šฉ ๋ถˆ๊ฐ€
 
  constructor(props) {
    // ๋ถ€๋ชจ ์ž์‹์„ ์ƒ์„ฑ์ž๋กœ ์—ฐ๊ฒฐ
    super(props);
    this.state = {
      a : 1
      ,b : this.props.name
    } 
    // ์ง์ ‘ ๋งŒ๋“ค์–ด์ค˜์•ผํ•จ  useState ์‚ฌ์šฉ ๋ถˆ๊ฐ€
  }

  render() {

    let {name, age} = this.props;
    let {a, b} = this.state;


    return (
      <div>
           props {name}
           props2 {age}
          <hr />
           props {a}
           props2 {b}
      </div>
    )
  }



}

export default ClassComponent;

 

 

  • class ์ƒ์† ๋ฐ›๋Š” ๊ฒฝ์šฐ
  • super() ํ‚ค์›Œ๋“œ๋Š” ํ•„์ˆ˜
  • this.state ๋กœ ์ง์ ‘ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค. (useState ์‚ฌ์šฉ ๋ถˆ๊ฐ€)

 

render() ๋ฉ”์„œ๋“œ์˜ ์—ญํ• 

  1. ์ปดํฌ๋„ŒํŠธ์˜ UI๋ฅผ ์ •์˜: render() ๋ฉ”์„œ๋“œ๋Š” JSX(JavaScript XML)๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋‚˜ํƒ€๋‚ ์ง€๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ด JSX๋Š” React์— ์˜ํ•ด HTML๋กœ ๋ณ€ํ™˜๋˜์–ด ๋ธŒ๋ผ์šฐ์ €์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.
  2. ๋™์  ๋ฐ์ดํ„ฐ ํ‘œ์‹œ: render() ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์ปดํฌ๋„ŒํŠธ์˜ props(๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ)์™€ state(์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ์ƒํƒœ)๋ฅผ ์‚ฌ์šฉํ•ด ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
  3. ์ž๋™ ์—…๋ฐ์ดํŠธ: render() ๋ฉ”์„œ๋“œ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋‚˜ props๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค React์— ์˜ํ•ด ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด UI๊ฐ€ ์ตœ์‹  ์ƒํƒœ๋ฅผ ๋ฐ˜์˜ํ•˜๋„๋ก ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.