๐ค ์์งlog ๐ค
[NextJS] NextJS ์์ Material-ui์ Styled-components ์ฌ์ฉํ๊ธฐ ๋ณธ๋ฌธ
๐ท Next JS
[NextJS] NextJS ์์ Material-ui์ Styled-components ์ฌ์ฉํ๊ธฐ
Eun_zii 2023. 3. 24. 15:03๐ ํ๋ก์ ํธ ์ธํ
- ํ์ํ ๋ํ๋์๋ฅผ ์ค์นํฉ๋๋ค.
npm i -D babel-plugin-styled-component
๐ .babelrc
- ๋จผ์ ๋ฐ๋ฒจ ํ์ผ์ ์์ ํฉ๋๋ค.
- ์๋ ํ์ผ์ next.js์ ssr๊ณผ ์ฐ๊ด์ด ์๋๋ฐ, ssr์ ์ํด styled-components ์คํ์ผ์ด ์ ์ฉ ์ ์ ํ๋ฉด ๋ ๋๋ง์ด ๋๋ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํจ์ ๋๋ค.
{
"presets": ["next/babel"],
"plugins": [
[
"babel-plugin-styled-components",
{ "fileName": true, "displayName": true, "pure": true, "ssr": true }
]
]
}
๐ _document.tsx
- babel ์ค์ ํ์ผ์ ์ค์ ํ๋ค๋ฉด ์ด์ _document ํ์ผ์ ์ค์ ํฉ๋๋ค. (์์ง styled-components๊ฐ ์๋ฒ๋ ๋๋ง์ ์ธํ ๋์ง ์์)
- mui๊ฐ nextjs์์ ์๋ํ๋๋ก ์ค์ ํ์๋๋ฐ ๊ทธ ์์ styled-components๋ ์ฌ์ฉ ํ๋๋ก ์ค์ ํฉ๋๋ค.
import React from "react";
import Document, { Html, Head, Main, NextScript } from "next/document";
import { ServerStyleSheet } from "styled-components";
import { ServerStyleSheets } from "@mui/styles";
export default class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
MyDocument.getInitialProps = async ctx => {
const sheet = new ServerStyleSheet();
const materialSheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props =>
sheet.collectStyles(materialSheets.collect(<App {...props} />))
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
};
} finally {
sheet.seal();
}
};
๐ ์ค๋ช
- ServerStyleSheet๋ฅผ ์ด์ฉํ์ฌ materialSheets๋ผ๋ ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค.
- materialSheets๋ฅผ ์ด์ฉํ์ฌ ์ง์ ํ ์ปดํฌ๋ํธ(ex: <App />)์ ์คํ์ผ ์์๋ฅผ ๊ฒ์ํ๊ณ ๊ทธ ์คํ์ผ์ <style>ํ๊ทธ๋ก ์ถ์ถํฉ๋๋ค.
- ์ถ์ถํ ๊ฒฐ๊ณผ๋ฌผ์ Document์ ์ ๋ฌํฉ๋๋ค.
- ์ด๋ ๊ฒ ๋๋ฉด ์๋ฒ์์ ๋ ๋๋ง๋๊ณ ์์ค ํ์ด์ง์์๋ ์คํ์ผ์ด ํ์๋ฉ๋๋ค.
๐ _app.tsx
- ThemeProvider๋ฅผ ์ฃผ์ ํ๊ณ theme๋ฅผ ์ฌ์ฉํ๋๋ก ์ธํ ํฉ๋๋ค.
import type { AppProps } from "next/app";
import CssBaseline from "@mui/material/CssBaseline";
import { ThemeProvider } from 'styled-components'
const theme = {
primary: 'green',
}
const App = (props: AppProps) => {
const { Component, pageProps } = props;
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
);
};
export default App;
๐ ๊ธ๋ก๋ฒ ์คํ์ผ ์ฌ์ฉํ๊ธฐ
- styled-components์์ ๊ธ๋ก๋ฒ ์คํ์ผ์ ์ฌ์ฉํ๋ ์์์ ๋๋ค
styles/global-styles.ts
- ๋จผ์ ๊ธ๋ก๋ฒ ์คํ์ผ์ ์ ์ํฉ๋๋ค.
// styles/global-styles.ts
import { createGlobalStyle } from "styled-components";
export const GlobalStyle = createGlobalStyle`
html,
body {
padding: 0;
margin: 0;
letter-spacing: -1px;
font-size: 15px;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
.txt-c {
text-align: center;
}
.txt-r {
text-align: right;
}
.txt-l {
text-align: left;
}
p {
margin: 0;
}
`;
๐ _app.tsx
- _app.tsx์ ์ฌ์ฉํ ๊ธ๋ก๋ฒ ์คํ์ผ์ ๋ฃ์ด์ค๋๋ค.
import type { AppProps } from "next/app";
import CssBaseline from "@mui/material/CssBaseline";
import { ThemeProvider } from 'styled-components'
import { GlobalStyle } from "styles/global-styles";
const theme = {
primary: 'green',
}
const App = (props: AppProps) => {
const { Component, pageProps } = props;
return (
<ThemeProvider theme={theme}>
<GlobalStyle />
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
);
};
export default App;
728x90
'๐ท Next JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[NextJS] NextJS ์์ Material-Ui ์ฌ์ฉํ๊ธฐ (0) | 2023.03.24 |
---|---|
[NEXTJS ] getStaticProps( ) ์ getStaticPaths( ) (0) | 2023.02.24 |
[NEXTJS] ์๋ฒ์ฌ์ด๋ ๋ ๋๋ง์ ๋ฐ์ํ๋ html ๋งค์นญ ๋ฌธ์ (0) | 2023.02.21 |
[NextJS] NextJS ์์ window ๊ฐ์ฒด๊ฐ ์๋ค๊ณ ํ ๋ (0) | 2023.01.05 |
[NextJS] NextJS์์ ์ปดํฌ๋ํธํ์์ผ๋ก SVG์ฌ์ฉํ๊ธฐ (0) | 2022.12.05 |