【发布时间】:2020-07-04 21:59:48
【问题描述】:
我在控制台中收到上述警告。我不明白为什么。我在index.js 文件中有两个匹配的<body> 标签。完整的警告是 -
Warning: Expected server HTML to contain a matching <body> in <div>.
in body (at pages/index.js:12)
in div (at pages/index.js:11)
in div (at pages/index.js:7)
in Index (at _app.js:8)
in MyApp
in Container (created by AppContainer)
in AppContainer
按照我正在使用的 index.js 文件。我正在使用 Next.js 和 tailwindcss。
import Head from "next/head"
import SideNavbar from "../components/SideNavbar"
import MainPage from "../components/MainPage"
const Index = () => {
return (
<div className="mx-auto">
<Head>
<title>Manchester United</title>
</Head>
<body className="min-h-screen">
<div className="flex flex-row">
<SideNavbar />
<MainPage>
<div className="text-center px-2 py-6 text-5xl font-bold text-gray-200">
Manchester United
</div>
</MainPage>
</div>
</body>
</div>
)
}
export default Index
如果我用<div> 标记替换<body> 标记,警告就会消失。我使用的两个自定义组件<SideNavbar /> 和<MainPage />,没有使用任何<body> 标签。
【问题讨论】:
-
在你的 _document.js 中你必须有 div 即
<body> <Main /> <NextScript /> </body>所以Main不能通过你的 _app.js 有另一个 body 标签 -
我没有
_document.js文件。我使用npm init next-app创建了这个应用程序。我确实有一个_app.js文件,我必须创建它才能使用 tailwindcss。
标签: javascript html reactjs next.js