props

Ceciliaยท2023๋…„ 1์›” 12์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
7/9
post-thumbnail

https://www.youtube.com/watch?v=DuJC2ATdGtw&list=PLZKTXPmaJk8J_fHAzPLH8CJ_HO_M33e7-&index=8


props๋ž€?


props๋Š” properties์˜ ์•ฝ์ž๋กœ ์†์„ฑ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.
state์™€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ์‚ดํŽด๋ณด์ž!


1) ์šฐ์„  App.js์— age={}๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ 

import "./App.css";
import Hello from "./component/Hello";
import Welcome from "./component/Welcome";

function App() {
  return (
    <div className="App">
      <Hello age={10} />  //๐Ÿ‘ˆ ์ถ”๊ฐ€
      <Hello age={20} />  //๐Ÿ‘ˆ ์ถ”๊ฐ€
      <Hello age={30} />  //๐Ÿ‘ˆ ์ถ”๊ฐ€
    </div>
  );
}

export default App;

2) Hello.js์˜ function Hello()์— props๋ฅผ ๋„ฃ์–ด์ค€ ๋’ค
{name} ์˜†์— ({props.age})๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

import { useState } from "react";

export default function Hello(props) {  //๐Ÿ‘ˆ props ์ถ”๊ฐ€
  console.log(props);
  const [name, setName] = useState("Mike");

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">
        {name}({props.age})์„ธ   //๐Ÿ‘ˆ ({props.age})์„ธ ์ถ”๊ฐ€
      </h2>
      <button
        onClick={() => {
          setName(name === "Mike" ? "Jane" : "Mike");
        }}
      >
        Change
      </button>
    </div>
  
}

๐Ÿ‘‰ console์—์„œ function Hello(props)๋ฅผ ํ™•์ธํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ‘‰ App.js์—์„œ props๋ฅผ ๋ฐ›์•„์™€ Hello.js์— ({props.age})๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ๋‹ค.



๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ์ˆ˜์ • ๊ฐ€๋Šฅํ•˜๋‹ค.

import { useState } from "react";

export default function Hello({ age }) {  //๐Ÿ‘ˆ {age}๋กœ ๋ณ€๊ฒฝ
  const [name, setName] = useState("Mike");
  
  return (
    <div>
      <h1>state</h1>
      <h2 id="name">
        {name}({age})์„ธ   //๐Ÿ‘ˆ {age}๋กœ ๋ณ€๊ฒฝ
      </h2>
      <button
        onClick={() => {
          setName(name === "Mike" ? "Jane" : "Mike");
        }}
      >
        Change
      </button>
    </div>
  );
}



๋„˜๊ฒจ๋ฐ›์€ props๊ฐ’ ๋ณ€๊ฒฝ? โŒ


๋„˜๊ฒจ๋ฐ›์€ props๊ฐ’์€ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
๋„˜๊ฒจ๋ฐ›์€ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.


props๊ฐ’ ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ props๊ฐ’์„ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ state๋กœ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•  ์ˆ˜๋Š” ์žˆ๋‹ค.

const [age, setAge] = useState(props.age); ์ด๋ ‡๊ฒŒ.

๋งŒ์•ฝ ๋„˜๊ฒจ๋ฐ›์€ props๊ฐ’์„ props.age=100;๊ณผ ๊ฐ™์ด ์–ต์ง€๋กœ ๋ณ€๊ฒฝ์‹œํ‚ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.


์˜ˆ์‹œ๋กœ, Change๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‚˜์ด๊ฐ€ +1 ๋˜๋„๋ก ์ž‘์„ฑํ•ด๋ณด์ž.

import { useState } from "react";

export default function Hello(props) {
  console.log(props);
  const [name, setName] = useState("Mike");
  const [age, setAge] = useState(props.age);

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">
        {name}({age})์„ธ
      </h2>
      <button
        onClick={() => {
          setName(name === "Mike" ? "Jane" : "Mike");
          setAge(age + 1);
        }}
      >
        Change
      </button>
    </div>
  );
}

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ๋‚˜์ด๊ฐ€ +1 ๋œ๋‹ค.
๋‹ค์‹œํ•œ ๋ฒˆ ์ž‘์„ฑํ•œ๋‹ค.

๐Ÿ“Œ props๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๊ฒŒ ์•„๋‹ˆ๋ผ, ๋„˜๊ฒจ๋ฐ›์€ props๋ฅผ state๋กœ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•˜๋Š” ์ค‘์ด๋‹ค.




props๋ฅผ ํ™œ์šฉํ•ด๋ณด์ž


1) age๋ฅผ App.js๋กœ๋ถ€ํ„ฐ props๋กœ ๋ฐ›์•„์™€ ๋น„๊ตํ•˜์—ฌ ํ™”๋ฉด์— ๋„์›Œ์ฃผ๊ธฐ

import { useState } from "react";

export default function Hello({ age }) {
  const [name, setName] = useState("Mike");
  const msg = age > 19 ? "์„ฑ์ธ์ž…๋‹ˆ๋‹ค." : "๋ฏธ์„ฑ๋…„์ž ์ž…๋‹ˆ๋‹ค.";
  //age๊ฐ€ 19๋ณด๋‹ค ๋งŽ์œผ๋ฉด "์„ฑ์ธ์ž…๋‹ˆ๋‹ค." : age๊ฐ€ 19๋ณด๋‹ค ์ ์œผ๋ฉด "๋ฏธ์„ฑ๋…„์ž ์ž…๋‹ˆ๋‹ค."

  return (
    <div>
      <h1>state</h1>
      <h2 id="name">
        {name}({age})์„ธ : {msg}
      </h2>
      <button
        onClick={() => {
          setName(name === "Mike" ? "Jane" : "Mike");
        }}
      >
        Change
      </button>
    </div>
  );
}

๐Ÿ“Œ ํ™”๋ฉด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐฑ์‹ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ญ์ƒ state์™€ props๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค.




2) ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ€์ง€๊ณ ์žˆ๋Š” state๋ฅผ props๋กœ ๋„˜๊ฒจ์ฃผ๊ธฐ

UserName.js๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

export default function UserName({ name }) {
  return <p>Hello, {name}</p>;
}

// export default function UserName(props) {
//   return <p>Hello, {props.name}</p>;
// } 
// ํ˜ผ์ž ์ž‘์„ฑํ•ด๋ณธ ์ฝ”๋“œ์ธ๋ฐ, ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ผ๊ณ  ์ดํ•ดํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Hello.js์— <UserName name={name} />์„ ๋„ฃ์–ด์ค€๋‹ค. ์ด๋ ‡๊ฒŒ.
Auto import ๋˜๋Š”๊ฑด ๋ค

return (
    <div>
      <h1>state</h1>
      <h2 id="name">
        {name}({age})์„ธ : {msg}
      </h2>
      <UserName name={name} />   //๐Ÿ‘ˆ ์ถ”๊ฐ€
      <button
        onClick={() => {
          setName(name === "Mike" ? "Jane" : "Mike");
        }}
      >
        Change
      </button>
    </div>
  );



๐Ÿ“Œ {name}: Hello.js ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” state์ง€๋งŒ UserName.js ์ปดํฌ๋„ŒํŠธ ์ž…์žฅ์—์„œ๋Š” props๋‹ค.


๊ทธ๋ฆฌ๊ณ  Change๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด {name}์ด ๊ฐ™์ด ๋ฐ”๋€Œ๋Š”๊ฒƒ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
Jane(10)์„ธ : ๋ฏธ์„ฑ๋…„์ž ์ž…๋‹ˆ๋‹ค.
Hello, Jane

profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

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