โœจJSY
article thumbnail

๐Ÿ”จ ์ปดํฌ๋„ŒํŠธ์˜ ์žฌ์‚ฌ์šฉ

 

React๋Š” component๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ๋ฐ์ดํ„ฐ๋งŒ ๋‹ฌ๋ผ์ง€๋ฉด์„œ ๋™์ผํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์˜ ์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ <Hobby /> ์— MY_HOBBY์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ  props๋กœ ์ „๋‹ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const MY_HOBBY = [
  {
    title : "Movie",
    description : "Watch movie at theater",
  },
  {
    title : "Running",
    description : "Run 3km",
  },
  {
    title : "Football",
    description : "Kick a ball with our team",
  },
];

 

1. spread operator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋กœ props๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

import Hobby from "./components/Hobby.jsx";
import { MY_HOBBY } from "./data.js";

function App() {
  return (
    <>
      {/*...codes...*/}
      
      <section>
        <ul>
          <Hobby {...MY_HOBBY[0]} />
          <Hobby {...MY_HOBBY[1]} />
          <Hobby {...MY_HOBBY[2]} />
       </ul>
      </section>
      
      {/*...codes...*/}
    </>
  );
}

export default App;

 

2. ์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ๋Š” ์ธ์ž๋กœ์„œ ์†์„ฑ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์ธ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

export default function Hobby({ title, description }) {
  return (
    <li>
      <h2>{title}</h2>
      <p>{desription}</p>
    </li>
  )
}

 

 

์ด ๋ฐฉ์‹๋„ ์ข‹์ง€๋งŒ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฉด ๋ Œ๋”๋ง ๊ณผ์ •์—์„œ ๊นจ์ง€๊ฑฐ๋‚˜ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

 

import Hobbies from "./components/Hobby.jsx";
import { MY_HOBBY } from "./data.js";

function App() {
  return (
    <>
      {/* codes */}
      
      <section>
        <Hobbies {...MY_HOBBY} />
      </section>
      
      {/* codes */}
    </>
  );
}

export default App;
export default function Hobbies() {
  return (
    <ul>
      {MY_HOBBY.map((hobby) => (
        <Hobby key={hobby.title} {...hobby} />
        )
      )}
    </ul>
  );
}

 

์ค‘๊ด„ํ˜ธ ์•ˆ์— map ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋กœ ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋™์ ์œผ๋กœ ๋ Œ๋”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โ›” WARNING
map ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์‹œ ๊ฐ ๋ฆฌ์ŠคํŠธ์—๋Š” ๊ณ ์œ ํ•œ key ์†์„ฑ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. React๋Š” ๋‚˜์ค‘์— ๋ฐ์ดํ„ฐ๋ฅผ insert, delete, reorder ํ•  ๋•Œ ์ฐธ์กฐํ•  key๊ฐ’์„ ํ•„์š”๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 


๐Ÿ”จ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ ํ•จ์ˆ˜๋ฅผ prop์œผ๋กœ ๋ฐ›๊ธฐ

on์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ(ex. onClick, onChange, ...)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

import ListButton from "./ListButton.jsx";

export default function ShowTemplate() {
  function handleSelect(selectedButton) {
    setSelectedContent(selectedButton);
  }
  
  return (
    <section>
      <ListButton
        isSelected={selectedContent === "movie"}
        onClick={() => handleSelect("movie")}
      >
      Movie
      </ListButton>
      <ListButton
        isSelected={selectedContent === "running"}
        onClick={() => handleSelect("running")}
      >
      Running
      </ListButton>
      <ListButton
        isSelected={selectedContent === "football"}
        onClick={() => handleSelect("football")}
      >
      Football
      </ListButton>
    </section>
  );
}
export default function listButton({ children, onSelect }) {
  return (
    <li>
      <button onClick={onSelect}>
        {children}
      </button>
    </li>
  );
}


1. ์ด๋ฒคํŠธ๋ฅผ ์ฒญ์ทจํ•˜๋Š” ํ•ด๋‹น ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์˜ html tag(์œ„์—์„œ๋Š” button์ด ์ด์— ํ•ด๋‹น) ์— on- prop์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

2. onClick์„ ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์ž์™€ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. (๊ด€์Šต์ ์œผ๋กœ onSelect)

3. onSelect๋Š” ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ์œผ๋กœ์„œ ์—ฐ๊ฒฐ๋˜๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ trigger ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’ก TIP
์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์˜ ํ•จ์ˆ˜์—์„œ ์ธ์ž๋ฅผ ํ•„์š”๋กœ ํ•  ๋•Œ ์“ธ ์ˆ˜ ์žˆ๋Š” ํŠธ๋ฆญ(?)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ

prop={ ( ) => handleProp(argument) }์™€ ๊ฐ™์ด ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์ต๋ช… ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ ์—†์ด ๊ทธ๋ƒฅ prop= { handleProp(argument) } ์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉด, ์ตœ์ดˆ ๋ Œ๋” ์ฆ‰์‹œ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, ์›๋ž˜ ์˜๋„์™€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค.


โž•

export default function ListButton({ children, ...props }) {
  return (
    <li>
      <button {...props}>
        {children}
      </button>
    </li>
  );
}

 

wrapper ์ปดํฌ๋„ŒํŠธ ์ž‘์„ฑ ์‹œ ์ข€ ๋” ์œ ์—ฐํ•˜๊ฒŒ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์Šคํ‚ฌ๋กœ {...props} ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
{...props}๋กœ ๋‚˜๋จธ์ง€ ์ธ์ž๋ฅผ ๋ชจ๋‘ ๊ฐ์ฒด๋กœ ํ•ฉ๋ณ‘ํ•ด ์ „๋‹ฌ๋ฐ›๊ณ , spread operator๋กœ์„œ์˜ {...props}๋ฅผ ๋นŒํŠธ์ธ ๊ฐ์ฒด์— ๋‹ฌ์•„์ฃผ๋ฉด
onSelect๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•˜๋Š” ๋“ฑ์˜ ๊ท€์ฐฎ์€ props ๋•์ง€๋•์ง€๋ฅผ ๊ตณ์ด ์•ˆ ์ ์–ด๋„ ๋˜์–ด ํŽธํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜€


๐Ÿ”จ ์กฐ๊ฑด์  ์ปจํ…์ธ  ๋ Œ๋”๋ง์˜ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

๊ฐ€๋…์„ฑ ๋ฐ ํšจ์œจ์„ฑ ์ธก๋ฉด์„ ๊ณ ๋ คํ•˜๋ฉฐ ์ ์ ˆํ•œ ์กฐ๊ฑด ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉ => ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์กฐ๊ฑด์ด ๋‘ ๊ฐœ ์ด์ƒ์ผ ๋•Œ

{isActive ? "active" : ""}


2. ์ค‘๊ด„ํ˜ธ ์•ˆ์— && ์—ฐ์‚ฐ์ž ์ด์šฉ => ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์กฐ๊ฑด์ด ํ•˜๋‚˜๋งŒ ์žˆ์„ ๋•Œ

{isActive && "active"}


3. ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  if ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌ => ์กฐ๊ฑด์‹์ด ๋งŽ์•„์ ธ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋  ๋•Œ

export default function HobbyQuestion() {
  let content = "<p>What's your Hobby?</p>";
  if (selectedContent) {
    content = (
      <div className="contents">
        <h3>Basketball</h3>
        <p>it's funny!</p>
      </div>
    );
  }
  
  {/* codes */}
  {content}
}

 

'FE > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[React] Million JS ๋Š” ์–ด๋–ป๊ฒŒ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™” ํ•˜๋Š”๊ฐ€?  (1) 2024.04.13
profile

โœจJSY

@JUNSANG YOO

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

๊ฒ€์ƒ‰ ํƒœ๊ทธ