小编Jel*_*den的帖子

NextJs - 将 HTML Lang 属性添加到对象表示法中的自定义 _document

我想<html lang="...">用对象表示法在 NextJs 应用程序上设置标签。

我为 Styled-components 渲染了一个 serverStylesheet,所以我当前的_document文件看起来像这样:

class CustomDocument extends Document {
static getInitialProps = async (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();
  }
}}
Run Code Online (Sandbox Code Playgroud)

我看过很多关于如何渲染常规 JSX 组件的文章,但我想用对象表示法来做。

reactjs next.js

3
推荐指数
1
解决办法
7166
查看次数

标签 统计

next.js ×1

reactjs ×1