
import React, { useState, useEffect } from "react";
import "./App.css";
import Customer from "./components/Customer";
import {
Paper,
Table,
TableHead,
TableBody,
TableRow,
TableCell,
CircularProgress,
} 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 StyledCircularProgress = styled(Table)({
margin: theme.spacing.unit * 2,
});
function App() {
const [customers, setCustomers] = useState("");
const [completed, setCompleted] = useState(0);
useEffect(() => {
const callApi = async () => {
const response = await fetch("/api/customers");
const body = await response.json();
return body;
};
callApi()
.then((res) => setCustomers(res))
.catch((err) => console.log(err));
const progress = () => {
completed >= 100 ? setCompleted(0) : setCompleted(completed + 1);
};
this.timer = setInterval(this.progress, 20);
}, []);
return (
<StyledPaper>
<StyledTable>
<TableHead>
<TableRow>
<TableCell>번호</TableCell>
<TableCell>이미지</TableCell>
<TableCell>이름</TableCell>
<TableCell>생년월일</TableCell>
<TableCell>성별</TableCell>
<TableCell>직업</TableCell>
</TableRow>
</TableHead>
<TableBody>
{customers ? (
customers.map((c) => (
<Customer
key={c.id} // map 을 사용하려면 key 라는 속성이 있어야 함(안하면 Console창에 에러가 발생)
id={c.id}
image={c.image}
name={c.name}
birthday={c.birthday}
gender={c.gender}
job={c.job}
/>
))
) : (
<TableRow>
<TableCell colSpan="6" align="center">
<CircularProgress
className={progress}
variant="determine"
value={this.state.completed}
/>
</TableCell>
</TableRow>
)}
</TableBody>
</StyledTable>
</StyledPaper>
);
}
theme 변수가 정의되지 않음progress 변수가 정의되지 않음theme 변수 정의:theme 변수는 MUI의 useTheme 훅을 사용하여 가져올 수 있습니다. styled 함수 내부에서 theme를 사용할 수 있도록 StyledCircularProgress 컴포넌트를 수정합니다.progress 변수 정의:CircularProgress 컴포넌트의 className에 할당된 progress 변수는 존재하지 않기 때문에 발생하는 오류입니다. 따라서 progress 클래스를 className에 직접 전달하지 않고, completed 상태를 직접 전달하도록 수정합니다.import React, { useState, useEffect } from "react";
import "./App.css";
import Customer from "./components/Customer";
import {
Paper,
Table,
TableHead,
TableBody,
TableRow,
TableCell,
CircularProgress,
} from "@mui/material";
import { styled, useTheme } from "@mui/system";
// Styled 컴포넌트 정의
const StyledPaper = styled(Paper)({
width: "100%",
marginTop: "24px",
overflow: "auto",
});
const StyledTable = styled(Table)({
minWidth: 1080,
});
const StyledCircularProgress = styled(CircularProgress)(({ theme }) => ({
margin: theme.spacing(2),
}));
function App() {
const [customers, setCustomers] = useState("");
const [completed, setCompleted] = useState(0);
const theme = useTheme();
useEffect(() => {
const callApi = async () => {
const response = await fetch("/api/customers");
const body = await response.json();
return body;
};
callApi()
.then((res) => setCustomers(res))
.catch((err) => console.log(err));
const progress = () => {
setCompleted((prevCompleted) =>
prevCompleted >= 100 ? 0 : prevCompleted + 1
);
};
const timer = setInterval(progress, 20);
return () => {
clearInterval(timer);
};
}, []);
return (
<StyledPaper>
<StyledTable>
<TableHead>
<TableRow>
<TableCell>번호</TableCell>
<TableCell>이미지</TableCell>
<TableCell>이름</TableCell>
<TableCell>생년월일</TableCell>
<TableCell>성별</TableCell>
<TableCell>직업</TableCell>
</TableRow>
</TableHead>
<TableBody>
{customers ? (
customers.map((c) => (
<Customer
key={c.id} // map 을 사용하려면 key 라는 속성이 있어야 함(안하면 Console창에 에러가 발생)
id={c.id}
image={c.image}
name={c.name}
birthday={c.birthday}
gender={c.gender}
job={c.job}
/>
))
) : (
<TableRow>
<TableCell colSpan="6" align="center">
<StyledCircularProgress
variant="indeterminate"
value={completed}
/>
</TableCell>
</TableRow>
)}
</TableBody>
</StyledTable>
</StyledPaper>
);
}
export default App;
import React, { useState, useEffect } from "react";
import "./App.css";
import Customer from "./components/Customer";
import {
Paper,
Table,
TableHead,
TableBody,
TableRow,
TableCell,
CircularProgress,
} from "@mui/material";
import { styled, useTheme } from "@mui/system";
// Styled 컴포넌트 정의
const StyledPaper = styled(Paper)({
width: "100%",
marginTop: "24px",
overflow: "auto",
});
const StyledTable = styled(Table)({
minWidth: 1080,
});
const StyledCircularProgress = styled(CircularProgress)(({ theme }) => ({
margin: theme.spacing(2),
}));
function App() {
const [customers, setCustomers] = useState("");
const [completed, setCompleted] = useState(0);
const theme = useTheme();
useEffect(() => {
const callApi = async () => {
const response = await fetch("/api/customers");
const body = await response.json();
return body;
};
callApi()
.then((res) => setCustomers(res))
.catch((err) => console.log(err));
const progress = () => {
setCompleted((prevCompleted) =>
prevCompleted >= 100 ? 0 : prevCompleted + 1
);
};
const timer = setInterval(progress, 20);
return () => {
clearInterval(timer);
};
}, []);
return (
<StyledPaper>
<StyledTable>
<TableHead>
<TableRow>
<TableCell>번호</TableCell>
<TableCell>이미지</TableCell>
<TableCell>이름</TableCell>
<TableCell>생년월일</TableCell>
<TableCell>성별</TableCell>
<TableCell>직업</TableCell>
</TableRow>
</TableHead>
<TableBody>
{customers ? (
customers.map((c) => (
<Customer
key={c.id} // map 을 사용하려면 key 라는 속성이 있어야 함(안하면 Console창에 에러가 발생)
id={c.id}
image={c.image}
name={c.name}
birthday={c.birthday}
gender={c.gender}
job={c.job}
/>
))
) : (
<TableRow>
<TableCell colSpan="6" align="center">
<StyledCircularProgress
variant="indeterminate"
value={completed}
/>
</TableCell>
</TableRow>
)}
</TableBody>
</StyledTable>
</StyledPaper>
);
}
export default App;
⇒ 데이터가 다 들어왔을 때
