Jon*_*uza 6 server-side-rendering next.js
我有一个用例,我必须在 SSR 期间向根元素添加一个类,可能是 body 或 _next。该类需要根据 getInitialProps 响应有条件/动态添加。我想知道在 Next.js 的当前状态下这是否可能。
import React from "react";
import NextDocument, { Html, Head, Main, NextScript } from "next/document";
class Document extends NextDocument {
static async getInitialProps(ctx) {
const initialProps = await NextDocument.getInitialProps(ctx);
// Determine if class name should be added
return {
...initialProps,
shouldShow: true,
};
}
render() {
return (
<Html>
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
{`#__next {
height: ${this.props.shouldShow ? "100%" : "0"}
}`}
</style>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default Document;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8935 次 |
| 最近记录: |