Props ๐Ÿพ

Bonnie Ryuยท2020๋…„ 11์›” 10์ผ
1

์ฐจ๊ทผ์ฐจ๊ทผ React ๐Ÿพ

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

1. Props

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

2. Props ๊ฐ์ฒด

  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ 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 State;
  • Parent.js ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.(๊ธฐ๋ณธ์ ์œผ๋กœ app.js๊ฐ€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.)
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ <Child/> ์ปดํฌ๋„ŒํŠธ๋ฅผ import ํ›„ <h1> ํƒœ๊ทธ ์•„๋ž˜์— ์œ„์น˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋ถ€๋ชจ์˜ state์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ <Child /> ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props๋ฅผ ํ†ตํ•ด ๋„˜๊ฒจ๋ณด๊ฒ ๋‹ค.
// Parent.js

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

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

  render() {
    return (
      <div>
        <h1>Parent Component</h1>
				<Child titleColor={this.state.color}/>
      </div>
    );
  }
}

export default State;
  • ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ props ๋กœ titleColor ์†์„ฑ์„ ์ƒ์„ฑํ–ˆ๋‹ค.
  • titleColor์˜ ๊ฐ’์œผ๋กœ this.state.color, ์ฆ‰ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state ๊ฐ์ฒด ์ค‘ color ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ props๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์˜ state ๊ฐ’์„ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด <Child /> ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์ „๋‹ฌ ๋ฐ›์€ props ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

props ๊ฐ์ฒด

  • state ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ปดํฌ๋„ŒํŠธ์˜ props๋Š” ๊ฐ์ฒด์ด๋‹ค.
  • ์ž์‹ ์ปดํฌ๋„ŒํŠธ์ธ Child.js ๋‚ด๋ถ€์—์„œ props ๊ฐ์ฒด๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์šธ๊นŒ?
// 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>
      </div>
    );
  }
}

export default State;
  • <Child /> ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— <h1> ํƒœ๊ทธ๊ฐ€ ์žˆ๊ณ  ํ•ด๋‹น ํƒœ๊ทธ์˜ ๊ธ€์ž ์ƒ‰์ƒ์„ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ state ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋„๋ก ํ•œ๋‹ค.
  • render ํ•จ์ˆ˜์™€ return ํ•จ์ˆ˜ ์‚ฌ์ด์—์„œ this.props ๊ฐ’์„ console.log๋กœ ํ™•์ธ!
  • ๊ฒฐ๊ณผ ํ™•์ธ ์‹œ props ๊ฐ์ฒด ์•ˆ์— ๋ถ€๋ชจ๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๊ฐ€ key-value ํ˜•ํƒœ๋กœ ๋‹ด๊ฒจ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
<h1 style={{color : this.props.titleColor}}>Child Component</h1>

// this : ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ
// this.props : ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ props ๊ฐ์ฒด
// this.props.titleColor : props ๊ฐ์ฒด์˜ ํŠน์ • key(titleColor)๊ฐ’. ์ฆ‰ "red"
  • ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ props ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” state ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ props ๊ฐ์ฒด์˜ ํŠน์ • ํ‚ค๊ฐ’, ์ฆ‰ this.props.titleColor ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค!

3. Props & event

props๋ฅผ ํ†ตํ•œ event handler ์ „๋‹ฌ

// Parent.js

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

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

  handleColor = () => {
    this.setState({
      color: 'blue'
    })
  }

  render() {
    return (
      <div>
        <h1>Parent Component</h1>
				<Child titleColor={this.state.color} changeColor={this.handleColor}/>
      </div>
    );
  }
}
export default State;
  • props ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด state ๋ฐ์ดํ„ฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ถ€๋ชจ์—์„œ ์ •์˜ํ•œ event handler ํ•จ์ˆ˜๋„ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • props์˜ changeColor ๊ฐ’์œผ๋กœ Parent ํ•จ์ˆ˜์—์„œ ์ •์˜ํ•œ handleColor ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋‹ค.
  • <Child /> ์ปดํฌ๋„ŒํŠธ์—์„œ ์–ด๋–ป๊ฒŒ props๋กœ ์ „๋‹ฌ๋ฐ›์€ handleColor ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž.
// 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>
				<button onClick={this.props.changeColor}>Click</button>
      </div>
    );
  }
}

export default State;
  • <Child /> ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— <button> ํƒœ๊ทธ๊ฐ€ ์žˆ๋‹ค.
  • ๋‹ค์Œ์˜ ์ˆœ์„œ์— ๋”ฐ๋ผ์„œ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
    • <button> ์š”์†Œ์—์„œ onClick ์ด๋ฒคํŠธ ๋ฐœ์ƒ
    • ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ this.props.changeColor ์‹คํ–‰
    • props ๊ฐ์ฒด์˜ changeColor, ์ฆ‰ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ handleColor ํ•จ์ˆ˜ ์‹คํ–‰
    • handleColor ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ setState ํ•จ์ˆ˜ ํ˜ธ์ถœ - state์˜ color ๊ฐ’์„ 'blue' ๋กœ ๋ณ€๊ฒฝ
    • render ํ•จ์ˆ˜ ํ˜ธ์ถœ
    • <Child /> ์ปดํฌ๋„ŒํŠธ์— ๋ณ€๊ฒฝ๋œ state ๋ฐ์ดํ„ฐ(this.state.color) ์ „๋‹ฌ
    • this.props.titleColor ๋ฅผ ๊ธ€์ž ์ƒ‰์ƒ์œผ๋กœ ์ง€์ •ํ•˜๋Š” <h1> ํƒ€์ดํ‹€ ์ƒ‰์ƒ ๋ณ€๊ฒฝ
profile
Ryuwisdom
post-custom-banner

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