Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

개발공부일지

React - learn - Passing Props to a Component (컴포넌트에 props 전달하기) 본문

React

React - learn - Passing Props to a Component (컴포넌트에 props 전달하기)

보람- 2023. 11. 28. 17:58

 

 

Passing Props to a Component

https://react.dev/learn/passing-props-to-a-component

 

Passing Props to a Component – React

The library for web and native user interfaces

react.dev

 

 

props 전달하기!!!

import { getImageUrl } from "./utils.js";

function Profile({ person, size = 80 }) {
    const imageSrc = getImageUrl(person);
    return (
        <section className="profile">
            <h2>{person.name}</h2>
            <img
                className="avatar"
                src={imageSrc}
                alt={person.name}
                width={size}
                height={size}
            />
            <ul>
                <li>
                    <b>Profession: </b>
                    {person.profession}
                </li>
                <li>
                    <b>Awards: {person.awards.length} </b>(
                    {person.awards.join(", ")})
                </li>
                <li>
                    <b>Discovered: </b>
                    {person.discovered}
                </li>
            </ul>
        </section>
    );
}

export default function Gallery() {
    return (
        <div>
            <h1>Notable Scientists</h1>
            <Profile
                person={{
                    imageId: "szV5sdG",
                    name: "Maria Skłodowska-Curie",
                    profession: "physicist and chemist",
                    discovery: "polonium (chemical element)",
                    awards: [
                        "Nobel Prize in Physics",
                        "Nobel Prize in Chemistry",
                        "Davy Medal",
                        "Matteucci Medal",
                    ],
                }}
            />
            <Profile
                person={{
                    imageId: "szV5sdG",
                    name: "Maria Curie",
                    profession: "physicist and chemist",
                    discovery: "polonium (chemical element)",
                    awards: ["Nobel Prize in Physics", "Matteucci Medal"],
                }}
            />
        </div>
    );
}
// utils.jsx

export function getImageUrl(person, size = "s") {
  return "https://i.imgur.com/" + person.imageId + size + ".jpg";
}