Props

1. ๊ฐœ์š”

๋ฆฌ์•กํŠธ App๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์„ ๋ถ•์–ด๋นต์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ถ•์–ด๋นต ํ‹€์€ ์ปดํฌ๋„ŒํŠธ, ๋ถ•์–ด๋นต ์† ์žฌ๋ฃŒ๋Š” props, ์™„์„ฑ๋œ ๋ถ•์–ด๋นต์€ ๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ๊ฐ€ ๋œ๋‹ค.

์ด์ค‘ props์— ๋”ฐ๋ผ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๋ผ ํ•ด๋„ ๋ชจ๋‘ ๋‹ค๋ฅธ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ„ ๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด์ ์€ ํ‹€์€ ๊ฐ™๋‹ค.

์ด์ฒ˜๋Ÿผ props๋Š” ๊ฐ™์€ ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ˆˆ์— ๋ณด์ด๋Š” ๊ธ€์ž๋‚˜ ์ƒ‰๊น” ๋“ฑ์˜ ์†์„ฑ์„ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์† ์žฌ๋ฃŒ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.


2. props๋Š” ์ฝ๊ธฐ ์ „์šฉ

All React components must act like pure functions with respect to their props.

๋ฆฌ์•กํŠธ ๊ณต์‹ ๋ฌธ์„œ๋Š” ์œ„์™€ ๊ฐ™์ด props์˜ ์„ฑ๊ฒฉ์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋‹ค. ๊ณต์‹๋ฌธ์„œ์—๋Š” pure functions์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ๊ทธ๋ ‡๋‹ค๋ฉด pure functions์ด๋ผ๋Š” ๊ฒƒ์€ ๋ฌด์—‡์ผ๊นŒ?

์•„๋ž˜์˜ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž

function sum(a, b) {
  return a + b;
}

์œ„์˜ ํ•จ์ˆ˜ sum์˜ ์ธ์ž a,b์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ฐ™์€ ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ํ•ญ์ƒ ๊ฐ™์€ ์ถœ๋ ฅ๊ฐ’์„ ๋‚ธ๋‹ค. ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋ฅผ Pureํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

๋ฐ˜๋Œ€๋กœ Pure์˜ ๋ฐ˜๋Œ€์ธ Impureํ•œ ํ•จ์ˆ˜์˜ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž

function widthdraw(account, amount) {
  account.total -= amount;
}

์œ„์˜ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰์ด ๋˜๋ฉด ์ž์‹ ์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์ˆ˜ํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค.

์ˆœ์ˆ˜ํ•จ์ˆ˜์™€ ์ˆœ์ˆ˜ํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ดค๋‹ค. ๋‹ค์‹œ ์ •๋ฆฌํ•˜์ž๋ฉด

๋ชจ๋“  ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” props๋ฅผ ์ง์ ‘ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ณ (์ฝ๊ธฐ ์ „์šฉ), ๊ฐ™์€ props์— ๋Œ€ํ•ด์„œ๋Š” ํ•ญ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ(๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ)๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.


3. props์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

Parents ์ปดํฌ๋„ŒํŠธ์—์„œ Child ์ปดํฌ๋„ŒํŠธ๋กœ name์ด๋ผ๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜์ž. ๊ทธ๋ ‡๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

//  Parents.js;
import React from "react";
import Child from "./Child";

const Parents = () => {
  return (
    <div>
      <Child name="HD" />
    </div>
  );
};

export default Parents;

// Child.js
import React from "react";

const Child = (props) => {
  return <div>์ œ ์ด๋ฆ„์€ {props.name}์ž…๋‹ˆ๋‹ค.</div>;
};

export default Child;

Child.js ๊ฐ™์ด Child ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌ๋˜๋Š” props๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•˜์—ฌ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. props๋Š” ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋ ๋‹ค๋ฉฐ, ๋งŒ์•ฝ name๊ฐ’์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด props.name์„ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค.


4. ์—ฌ๋ ค๊ฐœ์˜ props์™€ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น

Parents ์ปดํฌ๋„ŒํŠธ์—์„œ name๊ฐ’ ๋ฟ ์•„๋‹ˆ๋ผ age, region์„ Child ์ปดํฌ๋„ŒํŠธ์— ๋„˜๊ฒจ์ฃผ๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด๋ณด์ž. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋˜๋Š” props๋ฅผ ES6์— ๋“ฑ์žฅํ•œ ๊ตฌ์กฐ๋ถ„ํ•ด(๋น„๊ตฌ์กฐํ™”) ํ• ๋‹น ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด๋ณด์ž.

//  Parents.js;
import React from "react";
import Child from "./Child";

const Parents = () => {
  return (
    <div>
      <Child name="HD" age="29" region="ํ•œ๊ตญ" />
    </div>
  );
};

export default Parents;

// Child.js
import React from "react";

// ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ props๊ฐ’๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
const Child = ({ name, age, region }) => {
  return (
    <div>
      ์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {age}์ด๊ณ  {region}์—์„œ ์™”์Šต๋‹ˆ๋‹ค.
    </div>
  );
};

export default Child;

props์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์™€ ๋‹ค๋ฅด๊ฒŒ Child ์ปดํฌ๋„ŒํŠธ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•์„ ํ†ตํ•ด props๊ฐ’๋“ค์„ ๋ถˆ๋Ÿฌ์™”๋‹ค. ์ด์ „์—๋Š” props.๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์™”์ง€๋งŒ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์„ ํ†ตํ•ด ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.


5. defaultProps๋กœ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •

๋งŒ์•ฝ ์ „๋‹ฌ ๋ฐ›์€ props๊ฐ’์ด ์—†์œผ๋ฉด ์–ด๋–กํ• ๊นŒ? ์ด๋Ÿด๋•Œ๋Š” ์กฐ๊ฑด๋ถ€ ๋žœ๋”๋ง์„ ํ†ตํ•ด ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  defaultProps๋ฅผ ์ด์šฉํ•ด ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ฑด๋ถ€ ๋žœ๋”๋ง์€ ๋‹ค๋ฅธ ์ฑ•ํ„ฐ์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ณ  ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” defaultProps๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž.

//  Parents.js;
import React from "react";
import Child from "./Child";

const Parents = () => {
  return (
    <div>
      <Child name="HD" />
      <Child />
    </div>
  );
};

export default Parents;

// Child.js
import React from "react";

const Child = ({ name }) => {
  return <div>์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค.</div>;
};

Child.defaultProps = {
  name: "์ด๋ฆ„์„ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.",
};

export default Child;

Parents.js์—์„œ ๋‘๊ฐœ์˜ Child ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•˜๋‚˜๋Š” name๊ฐ’์ด ์žˆ๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๊ฐ’์ด ์—†๋‹ค.

name๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ Child.js์—์„œ๋Š” Child.defaultProps๋ฅผ ์‚ฌ์šฉํ•ด name์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค. ์•„๋ž˜๋Š” ์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ์ด๋‹ค.


6. props.children

์ปดํฌ๋„ŒํŠธ ํƒœ๊ทธ ์‚ฌ์ด์— ๋„ฃ๊ณ  ์‹ถ์€ ์•จ๋ฆฌ๋จผํŠธ ๋˜๋Š” ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋• props.children์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด๋„ ์ข‹๋‹ค.

์ปจํƒ ์ธ ์˜ ๋‚ด์šฉ์ด ๋‹ด๊ธด Content ์ปดํฌ๋„ŒํŠธ๊ฐ€ Wrapper ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ๊ทธ๋ ‡๋‹ค๋ฉด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค.

//  Wrapper.js
import React from "react";

const Wrapper = () => {
  return (
    <div style={{ backgroundColor: "red", color: "white" }}>
      <h1>Wrapper ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h1>
    </div>
  );
};

export default Wrapper;

//  Content.js
import React from "react";
import Wrapper from "./Wrapper";

const Content = () => {
  return <Wrapper>
        <h2>Content ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h2>
    </Wrapper>;
};

export default Content;

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž. Content ์ปดํฌ๋„ŒํŠธ๊ฐ€ Wrapper ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ์—ฌ์žˆ๋‹ค. Content ์ปดํฌ๋„ŒํŠธ์˜ Wrapper ์ปดํฌ๋„ŒํŠธ ์‚ฌ์ด์—๋Š” <h2>Content ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h2>์˜ ์•จ๋ฆฌ๋จผํŠธ๊ฐ€ ์žˆ์ง€๋งŒ ํ™”๋ฉด์—๋Š” ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์•˜๋‹ค.

์™œ ๊ทธ๋Ÿด๊นŒ? ๊ทธ ์ด์œ ๋Š” <h2>Content ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h2>๋„ ํ•˜๋‚˜์˜ props์ธ๋ฐ Wrapper ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•ด๋‹น props๋ฅผ ์–ด๋””์— ๋„ฃ์„์ง€ ์ •์˜ํ•˜์ง€ ์•Š์•„์„œ์ด๋‹ค. ์ด๋•Œ ํ•„์š”ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ props.children์ด๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ž.

//  Wrapper.js
import React from "react";

const Wrapper = ({children}) => {
  return (
    <div style={{ backgroundColor: "red", color: "white" }}>
      <h1>Wrapper ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h1>
      {children}
    </div>
  );
};

export default Wrapper;

//  Content.js
import React from "react";
import Wrapper from "./Wrapper";

const Content = () => {
  return <Wrapper>
        <h2>Content ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h2>
    </Wrapper>;
};

export default Content;

์ด๋ฒˆ์—๋Š” ์„ฑ๊ณต์ ์œผ๋กœ <h2>Content ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h2>์˜ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๋‹ค๋ฅธ์ ์€ ์ฐพ์•„๋ณด์ž.

Wrapper ์ปดํฌ๋„ŒํŠธ์˜ props๋ฅผ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์„ ํ•˜์—ฌ children๊ฐ’์„ ๊ฐ€์ ธ์™”๋‹ค. ๊ทธ๋ฆฌ๊ณ  <h1>Wrapper ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h1>์•„๋ž˜์— children๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ Content ์ปดํฌ๋„ŒํŠธ์˜ <h2>Content ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h2>์ด ๋ฐ”๋กœ ์•„๋ž˜์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.

Wrapper.js์—์„œ ์ฝ˜์†”๋กœ children๋ฅผ ์ฐ์–ด๋ณด์ž ๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ์ฒด์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

children์—๋Š” Content ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž‘์„ฑ๋œ ๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ๊ฐ€ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์กด์žฌํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์˜ํ•  ์ ์€ props.children์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ž„์˜์˜ ์ด๋ฆ„์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ์–ป์„ ์ˆ˜ ์—†๋‹ค.


7. Conclusion

props์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ˆœ์ˆ˜ํ•จ์ˆ˜๋ผ๋Š” ๊ฐœ๋…๋„ ํ•จ๊ป˜ ๋ฐฐ์› ๋‹ค. props๊ฐ’์ด ๋ณ€ํ™”ํ•˜์ง€ ์•Š๋Š” ๋‹ค๋Š” ๊ฒƒ์„ ์—ฌ๋Ÿฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ ์ด๋Ÿฐ ๊ฐœ๋…์ด ์ˆœ์ˆ˜ํ•จ์ˆ˜์™€ ์—ฐ๊ด€์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋„˜๊ฒจ๋ฐ›๋Š” props์˜ ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋‚˜๋Š” ์ง€๊ธˆ๊นŒ์ง€ ์กฐ๊ฑด๋ถ€ ๋žœ๋”๋ง์„ ํ†ตํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ธ defaultProps์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  ํ•œ ๋ฒˆ์ฏค ์‚ฌ์šฉํ•ด์„œ ๋ฆฌ์•กํŠธ์— ๋Œ€ํ•œ ์ง€์‹์„ ๋„“ํ˜€์•ผ ๊ฒ ๋‹ค. ์‚ฌ์‹ค typescript๋ฅผ ๋ฆฌ์•กํŠธ์— ๋„์ž…ํ•œ๋‹ค๋ฉด ํ›จ์”ฌ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ... ์—ญ์‹œ๋‚˜ typescript๋ฅผ ๋ฐฐ์šด์ง€ ์–ผ๋งˆ ์•ˆ๋˜์—ˆ๊ณ  ๊ทธ ๋˜ํ•œ ๊ธฐ๋ก์„ ๋‚จ๊ธฐ์ง€ ์•Š์•„ ๊ธฐ์–ต์€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค...ใ…Žใ…Ž props.children๋ฅผ ์ฝ˜์†”๋กœ ์ฒ˜์Œ ์ฐ์–ด๋ณด๋Š” ๊ฒƒ๋„ ์žฌ๋ฏธ์žˆ์—ˆ๋‹ค! ๊ทธ ์•ˆ์— ๋‚ด์šฉ์ธ ๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ๋ผ ์‹ ๊ธฐํ•˜๋‹ค~ typescript๋„ ์ •๋ฆฌ๋ฅผ ์ž˜ํ•˜์ž๐Ÿ˜€


์ฐธ๊ณ 

๋„์„œ - ์†Œํ”Œ์˜ ์ฒ˜์Œ ๋งŒ๋‚œ ๋ฆฌ์•กํŠธ ๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ Components์™€ Props 5. props ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ


๐Ÿ“… 2022-07-22

Last updated