现在我正在用react+nodejs+nextserver+express制作一个迷你项目
发生意外错误
我不知道为什么_document.js中会出现错误。
如果您知道原因,请告诉我,谢谢
error:
Error: "MyDocument.getInitialProps()" should resolve to an object with a "html" prop set with a valid html string
at Object.renderToHTML (C:\node_bird_44\front\node_modules\next\dist\next-server\server\render.js:338:15)
at process._tickCallback (internal/process/next_tick.js:68:7)
GET / 500 11240.415 ms - 21
[ event ] disposing inactive page(s): /, /_error
import React from 'react';
import Document , {Main, NextScript } from 'next/document';
import Helmet from 'react-helmet';
class MyDocument extends Document {
static getInitialProps(context) {
return { helmet: Helmet.renderStatic()};
}
render() {
const { htmlAttributes, bodyAttributes, ...helmet } = this.props.helmet;
const htmlAttrs = htmlAttributes.toComponent();
const bodyAttrs = bodyAtributues.toComponent();
return(
<html {...htmlAttrs}>
<head>
{Object.values(helmet).map(el => el.toComponent())}
</head>
<body {...bodyAttrs}>
<Main />
<NextScript />
</body>
</html>
);
}
}
export default MyDocument;
```
你错过了html
prop 需要由 getInitialProps 返回。您可以从文档初始道具中获取它,如下所示:
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps, helmet: Helmet.renderStatic() }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)