๐Ÿ“– React Props๋ž€?

์–ธ์ง€ยท2024๋…„ 10์›” 26์ผ

React

๋ชฉ๋ก ๋ณด๊ธฐ
4/13
post-thumbnail

1. Prop๋ž€?

  • React์—์„œ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํผ๋„ŒํŠธ๋“ค์—๊ฒŒ ๋งˆ์น˜ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋“ฏ์ด ์›ํ•˜๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ๋Š”๊ฒŒ ๊ฐ€๋Šฅ
    โ†’ ์ด๋•Œ ์ปดํผ๋„ŒํŠธ์— ์ „๋‹ฌ๋œ ๊ฐ’์„ Props๋ผ๊ณ  ํ•œ๋‹ค.(properties์˜ ์ค„์ž„๋ง)
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋“ฏ์ด ์ „๋‹ฌํ•˜๋Š” ๊ฐ’์— ๋”ฐ๋ผ ๊ฐ๊ฐ ๋‹ค๋ฅธ UI๋ฅผ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

1. Button์ด ๋‹ด๊ฒจ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ jsx ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

const Button = () => {
  return <button>ํด๋ฆญ</button>;
};
export default Button;

2. Button์ด ๋‹ด๊ฒจ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ App.jsx์— ์„ ์–ธํ•˜์—ฌ ๊ฐ€์ ธ์˜ค๊ธฐ

import Button from "./components/button";
function App() {
  return (
    <>
      <Button />
      <Button />
      <Button />
    </>
  );
}

3. Button์— text ๊ฐ’ ๋„ฃ๊ธฐ

function App() {
  return (
    <>
      <Button text={"๋ฉ”์ผ"}  color={"red"}/>
      <Button text={"์นดํŽ˜"} />
      <Button text={"๋ธ”๋กœ๊ทธ"} />
    </>
  );
}
const Button = (props) => {
  console.log(props);
  return <button>ํด๋ฆญ</button>;
};

โ†’ text์˜ ๊ฐ’์„ ๋ฐ›์œผ๋ ค๋ฉด ์ปดํฌ๋„ŒํŠธ์— ๋งค๊ฐœ๋ณ€์ˆ˜(props)๋ฅผ ์ž…๋ ฅํ•ด์ค˜์•ผํ•œ๋‹ค.

4. ์ฝ˜์†” ํ™•์ธํ•˜๊ธฐ

โ†’ ๊ฐ’์ด ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋‹ด๊ฒจ์„œ ์ „๋‹ฌ๋œ๋‹ค.
โ†’ Props ๊ฐ’๋“ค์ด properties๋กœ ํ•˜๋‚˜์”ฉ ๋“ค์–ด๊ฐ€์žˆ๋‹ค.

3. Props ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

1. default Props ์‚ฌ์šฉ (ํ˜„์žฌ ์ง€์›ํ•˜์ง€์•Š์Œ)

const Button = (props) => {
  console.log(props);
  return (
    <button style={{ color: props.color }}>
      {props.text} - {props.color.toUpperCase()}
    </button>
  );
};

function App() {
return (
<>
<Button text={"๋ฉ”์ผ"} color={"red"} />
<Button text={"์นดํŽ˜"} />
<Button text={"๋ธ”๋กœ๊ทธ"} />
</>
);
}

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑ์„ ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค
โ†’ ์ด์œ  : ์นดํŽ˜, ๋ธ”๋กœ๊ทธ์—๋Š” color ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.(Undefined)
โ†’ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ : ์ž๋™์œผ๋กœ ์„ค์ •๋  ๊ธฐ๋ณธ ๊ฐ’์„ ์„ค์ •

>```
Button.defaultProps = {
  color: "black",
};

โ†’ ๋ชจ๋“  ๋ฒ„ํŠผ์˜ defualt ๊ฐ’์€ black์œผ๋กœ ์ง€์ •

1-1. default Props ์„ ๋Œ€์ฒดํ•  ๋ฐฉ๋ฒ•

const Button = ({ text = "๊ธฐ๋ณธ ํ…์ŠคํŠธ", color = "black" }) => {
  console.log(text, color);
  return (
    <button style={{ color: color }}>
      {text} - {color.toUpperCase()}
    </button>
  );
};

โ†’ default ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ํ•จ๊ป˜ ์ž‘์„ฑ

2. ๊ฐ์ฒด์˜ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์ด์šฉ

  • ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น : ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ํ•ด์ฒดํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” JavaScript ํ‘œํ˜„์‹
  • Button ์ปดํฌ๋„ŒํŠธ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ props๊ฐ€ ์•„๋‹ˆ๋ผ text, color๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค
const Button = ({ text, color }) => {
  console.log(text, color);
  return (
    <button style={{ color: color }}>
      {text} - {color.toUpperCase()}
    </button>
  );
};

3.props๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ

function App() {
  const buttonProps = {
    text: "๋ฉ”์ผ",
    color: "red",
    a: 1,
    b: 2,
    c: 3,
  };
  return (
    <>
      <Button {...buttonProps} />
      <Button text={"์นดํŽ˜"} />
      <Button text={"๋ธ”๋กœ๊ทธ"} />
    </>
  );
}
++ ๊ฐ•์˜์—์„œ๋Š” ์˜ค๋ฅ˜์—†์ด ์ž˜ ๋‚ฌ์ง€๋งŒ ๋‚˜๋Š” text, color ๊ฐ’๋งŒ ๋‚˜์˜ค๊ณ  a,b,c ๊ฐ’์€ ๋‚˜์˜ค์ง€์•Š์•„์„œ

๊ทธ๋ƒฅ Button ์ปดํฌ๋„ŒํŠธ์— ๋งค๊ฐœ๋ณ€์ˆ˜(โ€ฆrest)๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ฐ’์ด ๋‚˜์˜ค๋„๋ก ํ•˜์˜€๋‹ค.

4. Children Props

  • <Button></Button> ์œผ๋กœ ์…€ํ”„ ํฌ๋กœ์ง•ํ•˜์ง€ ์•Š๊ณ  ๊ทธ ์•ˆ์— ์ž์‹ ํƒœ๊ทธ๋‚˜ ์ปดํฌ๋„ˆํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • JavaScript ๊ฐ’๋ฟ๋งŒ ์•„๋‹ˆ๋ผ HTML ์š”์†Œ๋‚˜ React ์ปดํฌ๋„ŒํŠธ๋„ ์ „๋‹ฌ ๊ฐ€๋Šฅ
    function App() {
      const buttonProps = {
        text: "๋ฉ”์ผ",
        color: "red",
      };
      return (
        <>
          <Button {...buttonProps} />
          <Button text={"์นดํŽ˜"} />
          <Button text={"๋ธ”๋กœ๊ทธ"}>
            <Header />
          </Button>
        </>
      );
    }
    --
    const Button = ({ text = "๊ธฐ๋ณธ ํ…์ŠคํŠธ", color = "black", children }) => {
      console.log(text, color);
      return (
        <button style={{ color: color }}>
          {text} - {color.toUpperCase()}
          {children}
        </button>
      );
    };

โ†’ Header๋Š” Button์˜ ์ž์‹์ด๋ฏ€๋กœ Button ์ปดํฌ๋„ŒํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜์— children ์ถ”๊ฐ€
โ†’ children ๊ฐ’์„ ์ถœ๋ ฅ

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