-
[MUI Table 커스텀하기] React, Styled-Componet, Material-UI, TypeScript에서 테이블 커스텀하기프론트엔드 개발자가 될거야./react 2023. 2. 4. 14:31
https://www.korbit.co.kr/ 위 사진은 가상화폐 거래소 코빗에 있는 테이블이다.
회사에서 프로젝트를 하면서 이런식으로 테이블을 그려내야했는데
테이블을 내가 원하는 레이아웃으로 커스텀하기에 꽤 복잡했기에.. 그 부분에 대해 블로깅을 해보려고 한다.
우선 나는 css는 Styled-Componet로 스타일을 주고 있고 css라이브러리로는 material ui를 쓴다.
Styled-Componet는 material ui를 쉽게 overloading 할 수 있어서 내가 좋아하는 조합이다 : )
MUI: The React component library you always wanted
MUI provides a simple, customizable, and accessible library of React components. Follow your own design system, or start with Material Design.
mui.com
https://styled-components.com/
styled-components
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅🏾
styled-components.com
1. Styled-Componet용 material ui install 하기
npm install @mui/material @emotion/react @emotion/styled
2. tsconfig.json 세팅하기
만약 타입스크립트를 쓰고 있다면 tsconfig.json에도 설정을 해줘야한다.
{ "compilerOptions": { + "paths": { + "@mui/styled-engine": ["./node_modules/@mui/styled-engine-sc"] + } }, }
3. App.tsx 세팅하기
Styled-Component에서 material ui를 overload해서 자유롭게 쓰려면
StyledEngineProvide로 감싸주어야한다.import { StyledEngineProvider } from "@mui/styled-engine"; function App() { const isDark = useRecoilValue(isHeaderDarkAtom); return ( <StyledEngineProvider injectFirst> <ThemeProvider theme={isDark ? darkTheme : lightTheme}> <GlobalStyle /> <Router /> <ReactQueryDevtools initialIsOpen /> </ThemeProvider> </StyledEngineProvider> ); }
4. Module not found: Can't resolve @emotion/react 에러가 뜬다면..
npm install @emotion/react npm install @emotion/core npm install @emotion/styled
emotion을 다운로드 해주면 된다.
5. MUI Table 커스텀하기
https://mui.com/material-ui/react-table/
React Table component - Material UI
Tables display sets of data. They can be fully customized.
mui.com
mui에서 원하는 테이블 레이아웃을 가져온다.
=> overloading 해준다.
const MyTableContainer = styled(TableContainer)` > table { > thead { > tr { > th { } } } > tbody { > tr { > th { > td { } } } } } `;
이런식으로 MyTableContainer에 mui의 TableContainer를 오버로딩해주고
nesting을 사용하여 커스텀해주면 잘 적용된다 : )
'프론트엔드 개발자가 될거야. > react' 카테고리의 다른 글
[react] react-router-dom Outlet으로 Tab메뉴 구현하기 (0) 2023.02.08 로그인페이지에 헤더 안보이게 하기 (with. useLocation) (0) 2023.02.04 [react-query] react-query는 왜 써야할까? (0) 2023.01.24 [react-query] 어드민 페이지 조회기능 react-query 도입하기 (0) 2023.01.24 [axios] fetch 대신 axios 사용하기, 내가 경험한 트러블슈팅 (0) 2023.01.15