使用高级 Chakra UI 组件更快地构建 💎

了解更多
跳到内容
文档演练场指南博客
赞助

在 Remix 中使用 Chakra

在 Remix 项目中安装 Chakra UI 的指南

模板

使用下面的 Remix 模板快速开始。

安装

1

安装依赖

npm i @chakra-ui/react @emotion/react
2

添加代码片段

代码片段是预构建组件,可帮助您更快地构建 UI。使用 @chakra-ui/cli,您可以将代码片段添加到您的项目中。

npx @chakra-ui/cli snippet add
3

设置 Emotion 缓存

使用 Remix 沙盒中的代码片段,您可以将 Emotion 缓存添加到您的应用程序中。

Emotion 缓存代码片段

4

更新 tsconfig

如果您正在使用 TypeScript,您需要更新 tsconfig 文件中的 compilerOptions 以包含以下选项

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "skipLibCheck": true
  }
}

如果您正在使用 JavaScript,请创建一个 jsconfig.json 文件并将上述代码添加到该文件中。

5

设置 Provider

在您的应用程序根目录使用 Provider 组件包裹您的应用程序。

此 Provider 包含以下内容

  • 来自 @chakra-ui/reactChakraProvider 用于样式系统
  • 来自 next-themesThemeProvider 用于颜色模式

app/root.tsx

import React from "react"
import ReactDOM from "react-dom/client"
import App from "./App"
import { Provider } from "@/components/ui/provider"

export default function App() {
  return (
    <Provider>
      <Outlet />
    </Provider>
  )
}
6

尽情享用!

结合代码片段和 Chakra UI 的基础组件的强大功能,您可以更快地构建 UI。

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

const Demo = () => {
  return (
    <HStack>
      <Button>Click me</Button>
      <Button>Click me</Button>
    </HStack>
  )
}

已知问题

在使用 Chakra UI 与 Remix 时,您可能会遇到以下问题

Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

这是与您浏览器中安装的扩展相关的已知问题。我们建议在无痕模式下测试您的应用程序,以查看问题是否仍然存在。

我们欢迎您贡献来修复此问题。

上一页

Next.js (Pages)

下一页

Storybook