【发布时间】:2020-12-17 15:57:51
【问题描述】:
我已经从下一个样式组件示例中复制了 _document.js,并且我正在使用样式组件文档中的 babel 插件,但我仍然收到错误。
_document.js
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"
]
]
}
周三刚开始使用 next ,所以该技术还相当新,但我查看了所有文档,这似乎是应该工作的,但我仍然收到错误,有什么想法吗?
【问题讨论】:
标签: javascript reactjs next.js server-side-rendering styled-components