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

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

Can't run Chakra UI with Next.js 13

问题

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

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

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

layout.tsx 文件如下:

import './globals.css'
import { Inter } from 'next/font/google'
import Providers from "@/app/chakra-provider";

export const metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
        <html lang="en">
            <Providers>
                {children}
            </Providers>
        </html>
  )
}

provider.tsx

'use client'

import { CacheProvider } from '@chakra-ui/next-js'
import { ChakraProvider } from '@chakra-ui/react'

export default function Providers({
    children
    }: {
    children: React.ReactNode
}) {
    return (
        <CacheProvider>
            <ChakraProvider>
                {children}
            </ChakraProvider>
        </CacheProvider>
    )
}

我在 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,

import &#39;./globals.css&#39;
import {Inter} from &#39;next/font/google&#39;
import Providers from &quot;@/app/chakra-provider&quot;;

export const metadata = {
  title: &#39;Create Next App&#39;,
  description: &#39;Generated by create next app&#39;,
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
        &lt;html lang=&quot;en&quot;&gt;
            &lt;Providers&gt;
                {children}
            &lt;/Providers&gt;
        &lt;/html&gt;
  )
}

provider.tsx

&#39;use client&#39;

import { CacheProvider } from &#39;@chakra-ui/next-js&#39;
import { ChakraProvider } from &#39;@chakra-ui/react&#39;

export default function Providers({
    children
    }: {
    children: React.ReactNode
}) {
    return (
        &lt;CacheProvider&gt;
            &lt;ChakraProvider&gt;
                {children}
            &lt;/ChakraProvider&gt;
        &lt;/CacheProvider&gt;
    )
}

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之后的应用程序顶层

export default function RootLayout({ children }: {
  children: React.ReactNode;
}) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body&gt;{children}&lt;/body&gt;
    &lt;/html&gt;
  );
}

另一种解决方案 #2

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

export const Example = {
  parameters: {
    nextjs: {
      appDirectory: true,
    },
  },
};
英文:

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

export default function RootLayout({ children }: {
  children: React.ReactNode;
}) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body&gt;{children}&lt;/body&gt;
    &lt;/html&gt;
  );
}

Another Solution #2

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

export const Example = {
  parameters: {
    nextjs: {
      appDirectory: true,
    },
  },
};

答案2

得分: 0

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

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

corePlugins: {
  preflight: false,
}

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

英文:

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.

corePlugins: {
  preflight: false,
}

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:

确定