Skip to content

Commit

Permalink
props를 통해 데이터 전달하기
Browse files Browse the repository at this point in the history
App.js의 데이터를 props를 이용해 ExpenseItem 컴포넌트에 전달한다. props를 사용함으로써 컴포넌트의 재사용성을 높일 수 있다.
  • Loading branch information
Yoonyesol committed Aug 28, 2023
1 parent 51bfbf0 commit 6587446
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
47 changes: 46 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
import ExpenseItem from "./components/ExpenseItem";

function App() {
const expenseDate = new Date(2023, 8, 28);
const expenseTitle = "Car Insurance";
const expenseAmount = 500000;

const expenses = [
{
id: "e1",
title: "Toilet Paper",
amount: 94.12,
date: new Date(2020, 7, 14),
},
{ id: "e2", title: "New TV", amount: 799.49, date: new Date(2021, 2, 12) },
{
id: "e3",
title: "Car Insurance",
amount: 294.67,
date: new Date(2021, 2, 28),
},
{
id: "e4",
title: "New Desk (Wooden)",
amount: 450,
date: new Date(2021, 5, 12),
},
];

return (
<div>
<h2>Let's get started!</h2>
<ExpenseItem/>
<ExpenseItem
title={expenses[0].title}
amount={expenses[0].amount}
date={expenses[0].date}
/>
<ExpenseItem
title={expenses[1].title}
amount={expenses[1].amount}
date={expenses[1].date}
/>
<ExpenseItem
title={expenses[2].title}
amount={expenses[2].amount}
date={expenses[2].date}
/>
<ExpenseItem
title={expenses[3].title}
amount={expenses[3].amount}
date={expenses[3].date}
/>
</div>
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/ExpenseItem.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "./ExpenseItem.css";

function ExpenseItem() {
function ExpenseItem(props) {
return (
<div className="expense-item">
<div>2023-08-28</div>
<div>{props.date.toISOString()}</div>
<div className="expense-item__description">
<h2>Car Insurance</h2>
<div className="expense-item__price">500,000원</div>
<h2>{props.title}</h2>
<div className="expense-item__price">${props.amount}</div>
</div>
</div>
);
Expand Down

2 comments on commit 6587446

@Yoonyesol
Copy link
Owner Author

@Yoonyesol Yoonyesol commented on 6587446 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비구조화 할당을 이용한 props 전달받기

😊 비구조화 할당: 객체에서 값을 추출하는 문법

function ExpenseItem({title, amount, date}) {
  return (
    <div className="expense-item">
      <div>{date.toISOString()}</div>
      <div className="expense-item__description">
        <h2>{title}</h2>
        <div className="expense-item__price">${amount}</div>
      </div>
    </div>
  );
}

위와 같은 코드도 정상적으로 동작한다.

@Yoonyesol
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 방식으로 Props 주고 받기 (배열 내 객체 전체를 props로 전달)

App.js

<ExpenseItem expense={expense[0]} />

ExpenseItem.js

function ExpenseItem(props) {
  return (
    <div className="expense-item">
      <div>{props.expense.date.toISOString()}</div>
      <div className="expense-item__description">
        <h2>{props.expense.title}</h2>
        <div className="expense-item__price">${props.expense.amount}</div>
      </div>
    </div>
  );
}

Please sign in to comment.