Props

<aside> ๐Ÿ‘‰๐Ÿฟ props ๋ฐ›์•„์˜ค๊ธฐ

ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

</aside>

// MyComponent.js
const MyComponent = props => {
	return <div>์ œ ์ด๋ฆ„์€ {props.name}์ž…๋‹ˆ๋‹ค.</div>
}
// App.js
const App = () => {
	return <MyComponent name={'๋‚จ๊ถ์ฒ '} />
}

<aside> ๐Ÿ‘‰๐Ÿฟ props์˜ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •

defaultProps๋กœ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

</aside>

MyComponents.defaultProps = {
  name: "์ฒ ",
};

<aside> ๐Ÿ‘‰๐Ÿฟ Children

ํƒœ๊ทธ ์‚ฌ์ด์˜ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” prop์ด๋‹ค

</aside>

const MyComponents = (props) => {
  return (
    <>
      <div>์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ด๋ฆ„์€ {props.name}์ž…๋‹ˆ๋‹ค.</div>
      <div>children ๊ฐ’์€ {props.children}์ž…๋‹ˆ๋‹ค.</div>
    </>
  );
};
const App = () => {
  return (
    <MyComponents name={"์ฒ "}>
      ๋ฆฌ์•กํŠธ
    </MyComponents>
  );
};

<aside> ๐Ÿ‘‰๐Ÿฟ ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น์œผ๋กœ props ์ถ”์ถœํ•˜๊ธฐ.

์œ„์—์„œ name์ด๋‚˜ children ์•ž์— props๋ฅผ ๋ถ™ํ˜€์„œ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ,

๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น์„ ์ด์šฉํ•˜๋ฉด name, children์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

</aside>

const MyComponents = ({ name, children, favoriteNumber }) => {
  return (
    <>
      <div>์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค.</div>
      <div>children ๊ฐ’์€ {children}์ž…๋‹ˆ๋‹ค.</div>
      <div>์ œ๊ฐ€ ์ข‹์•„ํ•˜๋Š” ์ˆซ์ž๋Š” {favoriteNumber}์ž…๋‹ˆ๋‹ค.</div>
    </>
  );
};

<aside> ๐Ÿ‘‰๐Ÿฟ proptype์„ ํ†ตํ•œ props ๊ฒ€์ฆํ•˜๊ธฐ

props์˜ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

isRequired๋ฅผ ์‚ฌ์šฉ์‹œ ํ•„์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์•„์•ผํ•˜๋Š” props๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

</aside>

MyComponents.propTypes = {
  name: PropTypes.string,
  favoriteNumber: PropTypes.number.isRequired,
};

<aside> ๐Ÿ‘‰๐Ÿฟ PropType์˜ ์ข…๋ฅ˜

๋” ๋งŽ์€ PropType์˜ ์ข…๋ฅ˜๊ฐ€ ์žˆ์œผ๋‹ˆ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์‚ฌ์šฉํ•˜์ž.

โ†’ ๊ณต์‹ ๋ฌธ์„œ ๋งํฌ

</aside>