TIL38 - React Props

์˜ค์ง€์ˆ˜ยท2021๋…„ 6์›” 25์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
21/26
post-thumbnail

๐Ÿ… Today's Goal

  • props์˜ ๊ฐœ๋…์„ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์„ค๋ช…ํ•˜๊ธฐ
  • props์˜ ๊ฐœ๋…์œผ๋กœ ๋ถ€๋ชจ ์š”์†Œ์˜ state๋ฅผ ์ž์‹ ์š”์†Œ์—์„œ ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜์‹œํ‚ฌ ์ˆ˜ ์žˆ์„๊นŒ?
  • props์˜ ๊ฐœ๋…์œผ๋กœ ์ž์‹์—์„œ ์ผ์–ด๋‚œ ์ด๋ฒคํŠธ๋กœ ๋ถ€๋ชจ์˜ state ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์„๊นŒ?

props

  • properties(์†์„ฑ)
  • ๋‚˜์™€ ๊ด€๋ จ๋œ ํŠน์ง•๋“ค์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‚˜๋Š” ์—ฌ์ž์ด๋‹ค., ๋‚˜๋Š” ์•„๋น ์˜ ์–ผ๊ตด์„ ๋‹ฎ์•˜๋‹ค. ๋‚ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ด๋Ÿฐ ์†์„ฑ๋“ค์€ ๋ถ€๋ชจ๋‹˜์—๊ฒŒ์„œ ํƒœ์–ด๋‚˜๋ฉด์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ๋“ค์ด๊ณ  ๋‚ด๊ฐ€ ์Šค์Šค๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.
  • props๋ž€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ด๋‹ค.
  • props๋ฅผ ํ†ตํ•ด์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ state์˜ ์ƒํƒœ๊ฐ’๊ณผ event handler๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ถ€๋ชจ์—๊ฒŒ์„œ ์•„๋ฌด๋Ÿฐ ์†์„ฑ๋„ ๋ฐ›์ง€ ์•Š์€ Child component์—์„œ console.log๋กœ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ฌด ๊ฐ’๋„ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š”๋‹ค. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ props๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ props๋ฅผ ์ „๋‹ฌํ•ด์ค˜์•ผ ํ•œ๋‹ค.

Child ์ปดํฌ๋„ŒํŠธ์— props๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

<\Child message="๋‚˜๋Š” ์ดˆ์ฝœ๋ฆฟ์„ ๋งค์šฐ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค." />

Child ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌ๋œ props๋ฅผ ๋ณด์—ฌ์ค„ ๋•Œ

console.log({this.props.message});

  • props๋Š” javascript ์ž๋ฃŒํ˜•์ด ๋ฌด์—‡์ด๋“ ์ง€ ๋‹ค ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.(๊ฐ์ฒด, ๋ฐฐ์—ด, ํ•จ์ˆ˜, Number ๋“ฑ๋“ฑ...)

๐Ÿค” ๋ถ€๋ชจ์˜ state์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ Child ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props๋กœ ๋„˜๊ธฐ๋Š” ๋ฐฉ๋ฒ•

// 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 />
      </div>
    );
  }
}

export default Parent;
  • ๋ถ€๋ชจ์˜ state์—๋Š” color: 'red' ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.
  • ์ด๊ฒƒ์„ Child ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props๋ฅผ ํ†ตํ•ด ๋„˜๊ฒจ๋ณด์ž
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} />
      </div>
    );
  }
}
  • titleColor๋ผ๋Š” ์†์„ฑ์— ๊ฐ’์œผ๋กœ this.state.color ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ฆ‰, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state ๊ฐ์ฒด ์ค‘์—์„œ color์˜ ๊ฐ’์„ Child ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•ด์ค€ ๊ฒƒ์ด๋‹ค.

๐Ÿค” Child ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์ „๋‹ฌ ๋ฐ›์€ props๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ• ๊นŒ?

// Child.js

import React from 'react';

class Child extends React.Component {
  render() {
    retun (
      <div>
        <h1 style={{color: this.props.titleColor}}>Child Component</h1>
      </div>
    );
  }
}

export default Child;
  • h1 ํƒœ๊ทธ์˜ ๊ธ€์ž์ƒ‰์„ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค.
  • ์•„๊นŒ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” titleColor๋ผ๋Š” ์†์„ฑ์„ Child ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•ด์คฌ๋‹ค.
  • ํ˜„์žฌ Child ์ปดํฌ๋„ŒํŠธ๋Š” props ๊ฐ์ฒด ์ค‘์—์„œ titleColor ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • h1 ํƒœ๊ทธ์˜ ์ƒ‰์ƒ์„ ๋ฐ”๊ฟ”์ฃผ๊ธฐ ์œ„ํ•ด์„œ style์— ์ธ๋ผ์ธ์œผ๋กœ css์˜ ์†์„ฑ ์ค‘ color๋ผ๋Š” ์†์„ฑ์„ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ›์•„์˜จ props.titleColor๋กœ ์„ค์ •ํ•ด์คฌ๋‹ค.

props๋ฅผ ํ†ตํ•œ 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'
    }
  }
  
  handleColor = () => {
    this.setState({
      color: 'blue'
    });
  }
  
  render() {
    retun (
      <div>
        <h1>Parent Component</h1>
        <Child titleColor={this.state.color} changeColor={this.handleColor} />
      </div>
    );
  }
}
  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ŸฌhandleColor()๋ฅผ props ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด Child ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์ž‡๋‹ค.
// Child.js

import React from 'react';

class Child extends React.Component {
  render() {
    return (
      <div>
        <h1 style={{color: this.props.titleColor}}>Child Component</h1>
        <button onClick={this.props.changeColor}>Click</button>
      </div>
    );
  }
}

export default Child;
  • <button> ์š”์†Œ์—์„œ onClick ์ด๋ฒคํŠธ ๋ฐœ์ƒ
  • ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ this.props.changeColor ์‹คํ–‰
  • props ๊ฐ์ฒด์˜ changeColor = ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ handleColor ํ•จ์ˆ˜ ์‹คํ–‰
  • handleColor ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ setState ํ•จ์ˆ˜ ํ˜ธ์ถœ - state์˜ color ๊ฐ’์„ 'blue'๋กœ ๋ณ€๊ฒฝ
  • render ํ•จ์ˆ˜ ํ˜ธ์ถœ
  • Child ์ปดํฌ๋„ŒํŠธ์— ๋ณ€๊ฒฝ๋œ state ๋ฐ์ดํ„ฐ(this.state.color) ์ „๋‹ฌ
  • this.props.titleColor๋ฅผ ๊ธ€์ž ์ƒ‰์ƒ์œผ๋กœ ์ง€์ •ํ•˜๋Š” h1 ํƒ€์ดํ‹€ ์ƒ‰์ƒ ๋ณ€๊ฒฝ
profile
My Moto:: ๋‚ด ์Šค์Šค๋กœ์™€ ๋”๋ถˆ์–ด ์ฃผ๋ณ€์—๊ฒŒ๋„ ์ข‹์€ ์˜ํ–ฅ์„ ํ–‰์‚ฌํ•˜๋„๋ก ์ ๊ฒ€ & ๋…ธ๋ ฅ..!!

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