// 중복 코드 😢
<button className="px-4 py-2 bg-blue-500 text-white rounded">
변경
</button>
<button className="px-4 py-2 bg-red-500 text-white rounded">
삭제
</button>
<button className="px-4 py-2 bg-green-500 text-white rounded">
추가
</button>
❌ 같은 코드 반복
❌ 수정할 때 여러 곳 변경 필요
❌ 유지보수 어려움
// components/Button.jsx
export default function Button({ children, color, onClick }) {
return (
<button
onClick={onClick}
className={`px-4 py-2 bg-${color}-500 text-white rounded
hover:bg-${color}-600`}
>
{children}
</button>
);
}
"use client";
import Button from "@/components/Button";
export default function Page() {
return (
<div className="flex gap-2">
<Button color="blue" onClick={() => alert("변경")}>
변경
</Button>
<Button color="red" onClick={() => alert("삭제")}>
삭제
</Button>
<Button color="green" onClick={() => alert("추가")}>
추가
</Button>
</div>
);
}
props로 데이터
전달
<Button
color="blue"
onClick={() => alert("클릭")}
>
변경
</Button>
function Button({
children, // "변경"
color, // "blue"
onClick // () => alert("클릭")
}) {
return (...)
}
컴포넌트에 데이터를 전달하는 방법
children: 태그
사이 내용
color,
onClick: 직접
전달한 값
// components/Card.jsx
export default function Card({
title,
content
}) {
return (
<div className="bg-white p-6
rounded-lg shadow">
<h3 className="text-xl font-bold
mb-2">
{title}
</h3>
<p className="text-gray-600">
{content}
</p>
</div>
);
}
import Card from "@/components/Card";
export default function Page() {
return (
<div className="flex flex-col gap-4">
<Card
title="프로필"
content="홍길동"
/>
<Card
title="나이"
content="25세"
/>
<Card
title="직업"
content="개발자"
/>
</div>
);
}
홍길동
25세
개발자
my-first-nextjs/
app/
page.jsx // 홈 (네비게이션)
text-change/
page.jsx // 텍스트 변경
toggle/
page.jsx // 토글
counter/
page.jsx // 카운터
layout.jsx
components/
Button.jsx // 재사용 버튼
Card.jsx // 재사용 카드
public/
package.json