在 Next.js 的新应用程序文件夹中设置 Chakra UI。

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

Setup Chakra UI in Next.js's new app folder

问题

我正在尝试在我的 Next.js v13.1.6 项目中使用 Chakra UI v2.4.9,按照Chakra UI与Next.js的逐步指南进行设置,但它不起作用。

我正在使用包含 app/pages/src/ 文件夹。如提供者设置所述,我创建了 _app.js 文件,内容如下:

// src/_app.js

import { ChakraProvider } from '@chakra-ui/react';

function MyApp({ Component, pageProps }) {
  return (
    <ChakraProvider>
      <Component {...pageProps} />
    </ChakraProvider>
  )
}

export default MyApp

然后,我尝试在我的 src/app/page.js 中使用 Chakra 元素,如下所示:

"use client"
import Image from 'next/image'
import { Inter } from '@next/font/google'
import styles from './page.module.css'
import { Button } from '@chakra-ui/react';

const inter = Inter({ subsets: ['latin'] })

export default function Home() {
  return <div>
    <h1>Hello Chakra UI</h1>
    <Button colorScheme={"red"}>CLICK ME</Button>
  </div>
}

它显示按钮,但没有任何样式,不像 Chakra UI 的 Button 那样带有红色配色方案。

我认为问题可能是 _app.js 文件的位置,它目前位于 src/_app.js,我尝试将该文件放到以下位置:

  • 项目根目录的 /_app.js
  • src 文件夹的根目录 /src/_app.js(当前位置)
  • 如 Chakra UI 文档所述的 /src/pages/_app.js
  • /src/app/_app.js

但仍然不起作用。

英文:

I am trying to use Chakra UI v2.4.9 with my Next.js v13.1.6 following the the Chakra UI step-up guide with Next.js, but it doesn't work.

I am using the src/ folder which contains app/ and pages/. as the provider step up says I created the _app.js file with the following content

// src/_app.js

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

function MyApp({ Component, pageProps }) {
  return (
    &lt;ChakraProvider&gt;
      &lt;Component {...pageProps} /&gt;
    &lt;/ChakraProvider&gt;
  )
}

export default MyApp

Then I tried to use Chakra elements in my src/app/page.js as follows:

&quot;use client&quot;
import Image from &#39;next/image&#39;
import { Inter } from &#39;@next/font/google&#39;
import styles from &#39;./page.module.css&#39;
import { Button } from &#39;@chakra-ui/react&#39;

const inter = Inter({ subsets: [&#39;latin&#39;] })

export default function Home() {
  return &lt;div&gt;
    &lt;h1&gt;Hello Chakra UI&lt;/h1&gt;
    &lt;Button colorScheme={&quot;red&quot;}&gt;CLICK ME&lt;/Button&gt;
  &lt;/div&gt;
}

It shows the button without any style, Not like the Chakra UI Button with the red color scheme.

I thought the issue was the _app.js file location that it is currently in src/_app.js and tried putting the file at

  • /_app.js at the project root
  • /src/_app.js root of src folder (current)
  • /src/pages/_app.js as the Chakra UI docs says
  • /src/app/_app.js

And it is still not working.

答案1

得分: 3

以下是翻译好的部分:

"It seems like the Chakra documentation only warned about the app directory, and the other part is for the pages folder, like in older versions. To use any context in the app folder, I suggest you read the context part of the new doc. Following that, you would do it like so:

// app/theme-provider.js

'use client';

import { ChakraProvider } from '@chakra-ui/react';

export default function ThemeProvider({ children }) {
  return (
    <ChakraProvider>
      {children}
    </ChakraProvider>
  );
}
// app/layout.js

import ThemeProvider from './theme-provider';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <ThemeProvider>{children}</ThemeProvider>
      </body>
    </html>
  );
}
// app/page.js

'use client';

import { Button } from "@chakra-ui/react";

export default function Home() {
  return (
    <div>
      <h1>Hello Chakra UI</h1>
      <Button colorScheme="red">CLICK ME</Button>
    </div>
  );
}
```"

<details>
<summary>英文:</summary>

It seems like the Chakra documentation only warned about the `app` directory, and the other part is for the `pages` folder, like in older versions. To use any context in the `app` folder, I suggest you read the [context](https://beta.nextjs.org/docs/rendering/server-and-client-components#context) part of the new doc. Following that, you would do it like so:

```react
// app/theme-provider.js &#128072;&#127997;

&#39;use client&#39;;

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

export default function ThemeProvider({ children }) {
  return (
    &lt;ChakraProvider&gt;
      {children}
    &lt;/ChakraProvider &gt;
  );
}
// app/layout.js &#128072;&#127997;

import ThemeProvider from &#39;./theme-provider&#39;;

export default function RootLayout({ children }) {
  return (
    &lt;html&gt;
      &lt;body&gt;
        &lt;ThemeProvider&gt;{children}&lt;/ThemeProvider&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  );
}
// app/page.js &#128072;&#127997;

&quot;use client&quot;;

import { Button } from &quot;@chakra-ui/react&quot;;

export default function Home() {
  return (
    &lt;div&gt;
      &lt;h1&gt;Hello Chakra UI&lt;/h1&gt;
      &lt;Button colorScheme={&quot;red&quot;}&gt;CLICK ME&lt;/Button&gt;
    &lt;/div&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月10日 15:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408122.html
匿名

发表评论

匿名网友

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

确定