
// VS Code 터미널
npm install @mui/material @emotion/react @emotion/styled

→ src/components/Customer.js
import React from "react";
import { TableRow } from "@mui/material";
import TableCell from "@mui/material";
function Customer(props) {
return (
<TableRow>
<TableCell>{props.id}</TableCell>
<TableCell>
<img src={props.image} alt="profile" />
</TableCell>
<TableCell>{props.name}</TableCell>
<TableCell>{props.birthday}</TableCell>
<TableCell>{props.gender}</TableCell>
<TableCell>{props.job}</TableCell>
</TableRow>
);
}
export default Customer;
→ src/App.js
import React, { Component } from "react";
import "./App.css";
import Customer from "./components/Customer";
import { Table } from "@mui/material";
import TableHead from "@mui/material";
import TableBody from "@mui/material";
import TableRow from "@mui/material";
import TableCell from "@mui/material";
const customers = [
{
id: 1,
image: "https://i.pravatar.cc/150?img=3",
name: "박창영",
birthday: "980630",
gender: "male",
job: "student",
},
{
id: 2,
image: "https://i.pravatar.cc/150?img=1",
name: "홍길동",
birthday: "901023",
gender: "male",
job: "cooker",
},
{
id: 3,
image: "https://i.pravatar.cc/150?img=10",
name: "김수현",
birthday: "950101",
gender: "male",
job: "actor",
},
];
function App() {
return (
<div>
{customers.map((customer) => (
<Customer
key={customer.id} // map 을 사용하려면 key 라는 속성이 있어야 함(안하면 Console창에 에러가 발생)
id={customer.id}
image={customer.image}
name={customer.name}
birthday={customer.birthday}
gender={customer.gender}
job={customer.job}
/>
))}
</div>
);
}
export default App;

→ 에러 이유
@mui/material에서 여러 컴포넌트를 가져올 때, 한 번에 묶어서 가져오는 것이 중요합니다. 각 모듈을 개별적으로 @mui/material에서 가져올 때는 잘못된 가져오기 방식으로 인해 오류가 발생할 수 있습니다.
import { TableRow } from "@mui/material";
import TableCell from "@mui/material";
import { Table, TableHead, TableBody } from "@mui/material"; 이렇게 각 모듈마다 @mui/material을 반복해서 불러오면, 존재하지 않는 모듈을 불러오려 할 때 오류가 발생합니다.import { Table, TableHead, TableBody, TableRow, TableCell } from "@mui/material"; 한 번의 import 문으로 필요한 모든 모듈을 불러오면, @mui/material 패키지에서 제공하는 모든 모듈을 정확하게 가져올 수 있습니다.@mui/material은 여러 컴포넌트를 제공하는 단일 패키지입니다. 따라서 필요한 모든 컴포넌트를 한 번에 가져오는 것이 올바른 방법입니다.import 문으로 필요한 모든 컴포넌트를 가져오면 코드가 더 간결하고 가독성이 높아집니다.이와 같이 올바른 import 방법을 사용하면, @mui/material을 사용하는 데 있어 발생할 수 있는 대부분의 오류를 피할 수 있습니다.

import React from "react";
import { TableRow, TableCell } from "@mui/material";
function Customer(props) {
return (
<TableRow>
<TableCell>{props.id}</TableCell>
<TableCell>
<img src={props.image} alt="profile" />
</TableCell>
<TableCell>{props.name}</TableCell>
<TableCell>{props.birthday}</TableCell>
<TableCell>{props.gender}</TableCell>
<TableCell>{props.job}</TableCell>
</TableRow>
);
}
export default Customer;
import React from "react";
import "./App.css";
import Customer from "./components/Customer";
import {
Paper,
Table,
TableHead,
TableBody,
TableRow,
TableCell,
} from "@mui/material";
import { styled } from "@mui/system";
// Styled 컴포넌트 정의
const StyledPaper = styled(Paper)({
width: "100%",
marginTop: "24px",
overflow: "auto",
});
const StyledTable = styled(Table)({
minWidth: 1080,
});
const customers = [
{
id: 1,
image: "https://i.pravatar.cc/150?img=3",
name: "박창영",
birthday: "980630",
gender: "male",
job: "student",
},
{
id: 2,
image: "https://i.pravatar.cc/150?img=1",
name: "홍길동",
birthday: "901023",
gender: "male",
job: "cooker",
},
{
id: 3,
image: "https://i.pravatar.cc/150?img=10",
name: "김수현",
birthday: "950101",
gender: "male",
job: "actor",
},
];
function App() {
return (
<StyledPaper>
<StyledTable>
<TableHead>
<TableRow>
<TableCell>번호</TableCell>
<TableCell>이미지</TableCell>
<TableCell>이름</TableCell>
<TableCell>생년월일</TableCell>
<TableCell>성별</TableCell>
<TableCell>직업</TableCell>
</TableRow>
</TableHead>
<TableBody>
{customers.map((customer) => (
<Customer
key={customer.id} // map 을 사용하려면 key 라는 속성이 있어야 함(안하면 Console창에 에러가 발생)
id={customer.id}
image={customer.image}
name={customer.name}
birthday={customer.birthday}
gender={customer.gender}
job={customer.job}
/>
))}
</TableBody>
</StyledTable>
</StyledPaper>
);
}
export default App;

