๐ŸŒ๋ฆฌ์•กํŠธ_State ์ƒํƒœ & Props ์†์„ฑ

chloeยท2020๋…„ 10์›” 17์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
1/16
post-thumbnail

โฃ๏ธState,Props

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

์šฐ๋ฆฌ๊ฐ€ ๋จผ์ € ์•Œ์•„์•ผํ•  ๊ฒƒ์€ Props์™€ State๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋…์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

State
์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ’
state๋Š” ํ™”๋ฉด์— ๋ณด์—ฌ์ค„ ์ปดํฌ๋„ŒํŠธ์˜ ์ •๋ณด(์ƒํƒœ)๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๊ฐ์ฒด

๋ณด์—ฌ์ง€๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๋ Œ๋” ํ•„์š”

import React from 'react';

class State extends React.Component {
  constructor() {
    super();//this๊ฐ€ state์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๊ฒŒํ•ด์ฃผ๋Š” ๊ฒƒ์ด super์˜ ์—ญํ• 
    this.state = {//์ปดํฌ๋„ŒํŠธ์˜ state๋Š” ๊ฐ์ฒด. color_key๊ฐ’, red_value๊ฐ’
      color: 'red'
    };
  }

  render() {
    return (
      <div>
        <h1>Class Component | State</h1>
      </div>
    );
  }
}

export default State;

State์—์„œ ์ƒํƒœ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ์ด์œ ๋Š”?
๊ฒฐ๊ตญ ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ์š”์†Œ์—์„œ ๊ทธ ์ƒํƒœ๊ฐ’์„ ๋ฐ˜์˜ํ•ด์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ํšจ์œจ์ ์œผ๋กœ ํ™”๋ฉด(UI)์— ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•จ์ด๋‹ค.

์‹ค์ œ ์‚ฌ์šฉ์˜ˆ์‹œ

import React, { Component } from 'react';

export class State extends Component {
  constructor() {
    super();
    this.state = {
      color: 'red'
    };
  }

  render() {
    return (
      <div>
        <h1 style={{ color: this.state.color }}>Class Component|                    State</h1> //this๋Š” ํ˜„์žฌ state์ปดํฌ๋„ŒํŠธ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ. ๊ฒฐ๊ตญ state๋ฅผ ํ†ตํ•ด ๋ฆฌํ„ด๋ฌธ ์•ˆ์— ์žˆ๋Š” UI๋ฅผ ๊ด€๋ฆฌ
      </div>
    );
  }
}

export default State;

Event &setState

import React, { Component } from 'react';

export class State extends Component {
  constructor() {
    super();
    this.state = {
      color: 'red'
    };
  }

  handleColor = () => {
    this.setState({
      color: 'blue'
    })// setState๊ฐ’ ํ†ตํ•ด state ๋ณ€๊ฒฝํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
  }

  render() {
    return (
      <div>
        <h1 style={{ color: this.state.color }}>Class Component | State</h1>
        <button onClick={this.handleColor}>Click</button>
// addEventListenerํ•  ํ•„์š”์—†์ด jsx์•ˆ์—๋‹ค๊ฐ€ onClickํ†ตํ•ด ์ด๋ฒคํŠธ ์ •์˜
this(์ปดํฌ๋„ŒํŠธ)์•ˆ์— handleColorํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ํ•ด์คŒ
      </div>
    );
  }
}

export default State;

๐Ÿงํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ setState?
setState๋Š” ์ƒํƒฏ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ๋‹ค.
seteState๋ฉ”์„œ๋“œ๋กœ ์ž…๋ ฅ๋œ ๊ฐ์ฒด๋Š” ๊ธฐ์กด ์ƒํƒฏ๊ฐ’๊ณผ ๋ณ‘ํ•ฉ๋œ๋‹ค. ๋˜ํ•œ ๋น„๋™๊ธฐ๋กœ ์ƒํƒฏ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

Props

  • Props๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ๋ฌผ๋ ค๋ฐ›๋Š” ์†์„ฑ. ์ด๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์™€ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌํ•˜๋Š” props๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋œ๋‹ค.
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๊ฐ์ฒด
    (state๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๊ฐ์ฒด)
    ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธํ•œํ…Œ ์ „๋‹ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋กœ, (์ž์‹์ž…์žฅ์—์„œ) ์ฝ๊ธฐ ์ „์šฉ
  • prop๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ state์˜ ์ƒํƒœ๊ฐ’,event handler๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.
// Parent.js

import React from 'react';
import Child from '../pages/Child/Child';

class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      color: 'red'
    };
  }

  render() {
    return (
      <div>
        <h1>Parent Component</h1>
			<Child titleColor={this.state.color}/>
//{this.state.color}์ด๊ฒŒ ๊ฐ์ฒด๊ฐ’์œผ๋กœ props์— ์ €์žฅ์ด ๋œ๋‹ค.
      </div>
    );
  }
}

export default State;
// Child.js

import React from 'react';

class Child extends React.Component {
  render() {
		// console.log(this.props);

    return (
      <div>
        <h1 style={{color : this.props.titleColor}}>Child Component</h1>
         /*this: ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ
               this.props: ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ props๊ฐ์ฒด
               this.props.titleColor: props๊ฐ์ฒด์˜ ํŠน์ • key๊ฐ’*/
         <button onClick={this.props.changeColor}>Click</button>
    </div> 
    );
  }
}

export default State;
profile
Front-end Developer ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

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