Browse Source

docs: update _document example in next.js (#43657)

pull/43454/merge
kiner-tang(文辉) 1 year ago
committed by GitHub
parent
commit
0ca05a7e39
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 87
      docs/react/use-with-next.en-US.md
  2. 89
      docs/react/use-with-next.zh-CN.md

87
docs/react/use-with-next.en-US.md

@ -60,46 +60,45 @@ If you are using the Pages Router in Next.js and using antd as your component li
```tsx
import Document, { Html, Head, Main, NextScript, DocumentContext } from 'next/document';
import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const cache = createCache();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => (
<StyleProvider cache={cache}>
<App {...props} />
</StyleProvider>
),
});
const initialProps = await Document.getInitialProps(ctx);
// 1.1 extract style which had been used
const style = extractStyle(cache, true);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{/* 1.2 inject css */}
<style dangerouslySetInnerHTML={{ __html: style }}></style>
</>
const MyDocument = () => (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
MyDocument.getInitialProps = async (ctx: DocumentContext) => {
const cache = createCache();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => (
<StyleProvider cache={cache}>
<App {...props} />
</StyleProvider>
),
};
}
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
});
const initialProps = await Document.getInitialProps(ctx);
// 1.1 extract style which had been used
const style = extractStyle(cache, true);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{/* 1.2 inject css */}
<style dangerouslySetInnerHTML={{ __html: style }}></style>
</>
),
};
};
export default MyDocument;
```
3. Supports custom themes
@ -117,15 +116,7 @@ const withTheme = (node: JSX.Element) => (
},
}}
>
<ConfigProvider
theme={{
token: {
borderRadius: 16,
},
}}
>
{node}
</ConfigProvider>
{node}
</ConfigProvider>
</>
);

89
docs/react/use-with-next.zh-CN.md

@ -62,46 +62,45 @@ export default Home;
```tsx
import Document, { Html, Head, Main, NextScript, DocumentContext } from 'next/document';
import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const cache = createCache();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => (
<StyleProvider cache={cache}>
<App {...props} />
</StyleProvider>
),
});
const initialProps = await Document.getInitialProps(ctx);
// 1.1 extract style which had been used
const style = extractStyle(cache, true);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{/* 1.2 inject css */}
<style dangerouslySetInnerHTML={{ __html: style }}></style>
</>
const MyDocument = () => (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
MyDocument.getInitialProps = async (ctx: DocumentContext) => {
const cache = createCache();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => (
<StyleProvider cache={cache}>
<App {...props} />
</StyleProvider>
),
};
}
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
});
const initialProps = await Document.getInitialProps(ctx);
// 1.1 extract style which had been used
const style = extractStyle(cache, true);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{/* 1.2 inject css */}
<style dangerouslySetInnerHTML={{ __html: style }}></style>
</>
),
};
};
export default MyDocument;
```
3. 支持自定义主题
@ -119,15 +118,7 @@ const withTheme = (node: JSX.Element) => (
},
}}
>
<ConfigProvider
theme={{
token: {
borderRadius: 16,
},
}}
>
{node}
</ConfigProvider>
{node}
</ConfigProvider>
</>
);
@ -249,6 +240,6 @@ const HomePage: React.FC = () => (
export default HomePage;
```
> 注意: 上述方式没有在页面中使用如:`Select.Option` 、 `Typography.Text` 等子组件,因此可以正常使用。但如果你的页面中有使用类似这样的子组件,目前在 Next.js 中会看到如下警告:`Error: Cannot access .Option on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.`,目前需等待 Next.js 官方解决。再次之前,如果你的页面中使用了上述子组件,可在页面组件第一行加上 `"use client";` 来避免警告。更多细节可以参考示例:[with-sub-components](https://github.com/ant-design/ant-design-examples/blob/main/examples/with-nextjs-app-router-inline-style/src/app/with-sub-components/page.tsx)。
> 注意: 上述方式没有在页面中使用如:`Select.Option` 、 `Typography.Text` 等子组件,因此可以正常使用。但如果你的页面中有使用类似这样的子组件,目前在 Next.js 中会看到如下警告:`Error: Cannot access .Option on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.`,目前需等待 Next.js 官方解决。在此之前,如果你的页面中使用了上述子组件,可在页面组件第一行加上 `"use client";` 来避免警告。更多细节可以参考示例:[with-sub-components](https://github.com/ant-design/ant-design-examples/blob/main/examples/with-nextjs-app-router-inline-style/src/app/with-sub-components/page.tsx)。
更多详细的细节可以参考 [with-nextjs-app-router-inline-style](https://github.com/ant-design/ant-design-examples/tree/main/examples/with-nextjs-app-router-inline-style)。

Loading…
Cancel
Save