在 Remix 中使用 Chakra
在 Remix 项目中安装 Chakra UI 的指南
模板
使用下面的 Remix 模板快速开始。
安装
1
安装依赖
npm i @chakra-ui/react @emotion/react
2
3
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/react
的ChakraProvider
用于样式系统 - 来自
next-themes
的ThemeProvider
用于颜色模式
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.
这是与您浏览器中安装的扩展相关的已知问题。我们建议在无痕模式下测试您的应用程序,以查看问题是否仍然存在。
我们欢迎您贡献来修复此问题。