Props(Properties)๋ž€?

๐Ÿ“„ Props ๋ž€?

Props๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ์ •์—์„œ ํŠน์ • ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • Props๋Š” ์™ธ๋ถ€์—์„œ ์ œ๊ณต๋ฐ›์€ ๊ฐ’์„ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— ๊ฐ’์„ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.(๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„)
  • ํ”„๋กœํผํ‹ฐ๋Š” ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ“„ ์™œ Props๋ฅผ ์‚ฌ์šฉํ• ๊นŒ?

๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ๋ฐ์ดํ„ฐ์— ๋งž๊ฒŒ UI๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

function App() {
  return (
    <Wrapper>
      <Hello name="react" color="red" />
      <Hello color="pink" />
    </Wrapper>
  );
}

export default App;

Hello์ปดํฌ๋„ŒํŠธ์—์„œ ํƒœ๊ทธ์˜ ์†์„ฑ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ๋ผ๋ฏธํ„ฐ์— props์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

import React from "react";

function Hello(props) {
  // ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘
  console.log(props);
  return (
    <div
      style=
    >
      ์•ˆ๋…•ํ•˜์„ธ์š” {props.name}
    </div>
  );
}

Hello.defaultProps = {
  // ํŠน์ •๊ฐ’์„ ๋น ๋œจ๋ ธ์„๋•Œ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฐ’
  name: "์ด๋ฆ„์—†์Œ",
};
export default Hello; // Hello๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚ด๋ณด๋‚ธ๋‹ค.

๐Ÿ“„ props.children

์ปดํฌ๋„ŒํŠธ ํƒœ๊ทธ ์‚ฌ์ด์— ๋„ฃ์€ ๊ฐ’์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋• props.children์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

import React from "react";

function Wrapper({ children }) {
  const style = {
    border: "2px solid black",
    padding: "16px",
  };
  return <div style={style}>{children}</div>;
}

export default Wrapper;

์ „๋‹ฌ๋œ ์ธ์ž๋“ค์ด ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ฌถ์–ด์ ธ์„œ LikeButton์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ this.props๋กœ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

Leave a comment