【发布时间】:2021-01-27 20:31:06
【问题描述】:
在我的 NextJS React 应用程序中,当我更改代码中的某些内容时,HMR 工作并显示正确的更新,但如果我刷新页面,此错误再次出现。这发生在开发模式下。 注意到很多主题在哪里出现此错误,并毫不费力地尝试了一整天不同的配置设置。
请帮助我摆脱错误。
错误:
警告:道具
className不匹配。服务器:“sc-cBoprd hjrjKw” 客户端:“sc-iCoHVE daxLeG”
使用 "babel-plugin-styled-components": "1.11.1"
可能与问题相关的文件:
_App.tsx
function MyApp({ Component, pageProps, mainController }) {
return (
<ConfigurationProvider configuration={configuration}>
<ThemeProvider theme={mainController.getTheme()}>
<Normalize />
<Font />
<Component {...pageProps} controller={mainController} />
</ThemeProvider>
</ConfigurationProvider>
);
}
export default appControllerContext(MyApp);
_document.tsx
import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
}
.babelrc
{
"presets": [
"next/babel"
],
"plugins": [
[
"babel-plugin-styled-components",
{
"ssr": true,
"displayName": true,
"preprocess": false
}
]
]
}
【问题讨论】:
-
您收到的错误是由于只有客户端的代码也在服务器上呈现。能贴出整棵树吗?您收到错误的 ConfigurationProvider、Normalize、字体和页面内容。
标签: javascript reactjs babeljs next.js styled-components