无法在Next.js 13中运行Chakra UI。

huangapple go评论104阅读模式
英文:

Can't run Chakra UI with Next.js 13

问题

我在查看附加链接中的 Chakra UI 安装与 Next.js 13 时,我相信我已经完全完成了。

然而,当我保存我的文件后,我注意到自动页面没有刷新,我遇到了一些水合错误。在控制台中看到的错误代码如下:

无法在Next.js 13中运行Chakra UI。

layout.tsx 文件如下:

  1. import './globals.css'
  2. import { Inter } from 'next/font/google'
  3. import Providers from "@/app/chakra-provider";
  4. export const metadata = {
  5. title: 'Create Next App',
  6. description: 'Generated by create next app',
  7. }
  8. export default function RootLayout({
  9. children,
  10. }: {
  11. children: React.ReactNode
  12. }) {
  13. return (
  14. <html lang="en">
  15. <Providers>
  16. {children}
  17. </Providers>
  18. </html>
  19. )
  20. }

provider.tsx

  1. 'use client'
  2. import { CacheProvider } from '@chakra-ui/next-js'
  3. import { ChakraProvider } from '@chakra-ui/react'
  4. export default function Providers({
  5. children
  6. }: {
  7. children: React.ReactNode
  8. }) {
  9. return (
  10. <CacheProvider>
  11. <ChakraProvider>
  12. {children}
  13. </ChakraProvider>
  14. </CacheProvider>
  15. )
  16. }

我在 Stack Overflow 上查找了关于这个问题的信息,有人提到在替换
<body className={inter.className}>{children}</body> 行之后,他遇到了相似的问题。我现在也遇到了类似的问题。

英文:

I was looking the Chakra UI installation with Next.js 13 from the attached link and I believe did it completely.

However, when I saved my file afterwards, I noticed that the automatic page did not refresh and I encountered a hydration error in places. The error code seen in the console is as follows:

无法在Next.js 13中运行Chakra UI。

layout.tsx file in below,

  1. import &#39;./globals.css&#39;
  2. import {Inter} from &#39;next/font/google&#39;
  3. import Providers from &quot;@/app/chakra-provider&quot;;
  4. export const metadata = {
  5. title: &#39;Create Next App&#39;,
  6. description: &#39;Generated by create next app&#39;,
  7. }
  8. export default function RootLayout({
  9. children,
  10. }: {
  11. children: React.ReactNode
  12. }) {
  13. return (
  14. &lt;html lang=&quot;en&quot;&gt;
  15. &lt;Providers&gt;
  16. {children}
  17. &lt;/Providers&gt;
  18. &lt;/html&gt;
  19. )
  20. }

provider.tsx

  1. &#39;use client&#39;
  2. import { CacheProvider } from &#39;@chakra-ui/next-js&#39;
  3. import { ChakraProvider } from &#39;@chakra-ui/react&#39;
  4. export default function Providers({
  5. children
  6. }: {
  7. children: React.ReactNode
  8. }) {
  9. return (
  10. &lt;CacheProvider&gt;
  11. &lt;ChakraProvider&gt;
  12. {children}
  13. &lt;/ChakraProvider&gt;
  14. &lt;/CacheProvider&gt;
  15. )
  16. }

I was browsing about this on Stack&nbsp;Overflow, and someone mentioned that he experienced this after replacing the
&lt;body className={inter.className}&gt;{children}&lt;/body&gt; line
with just {children}. I am having a similar problem right now.

答案1

得分: 0

顶级布局称为根布局。此必需布局在应用程序中的所有页面之间共享。根布局必须包含html和body标记。

在搜索后,我认为这可能是问题
所以确保body标记出现在html之后的应用程序顶层

  1. export default function RootLayout({ children }: {
  2. children: React.ReactNode;
  3. }) {
  4. return (
  5. &lt;html lang=&quot;en&quot;&gt;
  6. &lt;body&gt;{children}&lt;/body&gt;
  7. &lt;/html&gt;
  8. );
  9. }

另一种解决方案 #2

在此找到解决方案https://storybook.js.org/blog/integrate-nextjs-and-storybook-automatically/下的next/navigation

  1. export const Example = {
  2. parameters: {
  3. nextjs: {
  4. appDirectory: true,
  5. },
  6. },
  7. };
英文:

The top-most layout is called the Root Layout. This required layout is shared across all pages in an application. Root layouts must contain html and body tags.

After some searching I think this maybe the problem
So make sure body tag is present in top-level of the app after html

  1. export default function RootLayout({ children }: {
  2. children: React.ReactNode;
  3. }) {
  4. return (
  5. &lt;html lang=&quot;en&quot;&gt;
  6. &lt;body&gt;{children}&lt;/body&gt;
  7. &lt;/html&gt;
  8. );
  9. }

Another Solution #2

Found the solution here https://storybook.js.org/blog/integrate-nextjs-and-storybook-automatically/ under next/navigation

  1. export const Example = {
  2. parameters: {
  3. nextjs: {
  4. appDirectory: true,
  5. },
  6. },
  7. };

答案2

得分: 0

实际上这不是一个问题。这是因为你正在使用 Tailwind,它重置了 CSS。

只需将以下代码添加到你的 tailwind.config.js 文件中:

  1. corePlugins: {
  2. preflight: false,
  3. }

更多详情请查看:解决方案问题

英文:

Acutally it was not an issue. it is because of you are using tailwind which reseting the css.

Just add this below code in your tailwind.config.js file.

  1. corePlugins: {
  2. preflight: false,
  3. }

For more details: Solution issue

huangapple
  • 本文由 发表于 2023年6月30日 04:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定