reactjs - Material-UI 版本 5 中的主题类型是什么?
问题描述
我正在将我的项目更新到MUI 版本 5。推荐使用情感 CSS。我想使用该theme
物业。现在打字稿要求输入theme
. 下面的版本不起作用。如何提供类型?
import { Theme } from "@mui/material"
const Root = styled('div')(({ theme: Theme }) => ({
background: theme.palette.grey[50],
}))
解决方案
如果您碰巧从 导入styled
API @mui/styles
,那么它不会工作,因为它没有提供开箱即用的 MUI 主题。您需要将createTheme
其传递给ThemeProvider
自己,然后增加类型,DefaultTheme
因为默认情况下它是一个空接口:
import { Theme } from '@mui/material/styles';
declare module '@mui/styles' {
interface DefaultTheme extends Theme {}
}
但请注意,不建议使用 legacy 包@mui/styles
。请参阅我的其他答案的更多详细信息。
如果您已经styled
从以下位置导入@mui/material/styles
:由于您使用的是打字稿,请删除Theme
类型,该styled
函数可以theme
在回调中推断属性的类型,因此您无需执行任何操作:
const Root = styled('div')(({ theme }) => ({
background: theme.palette.grey[50],
}))
但理论上,如果theme
没有类型,这就是你添加它的方式:
const Root = styled('div')(({ theme }: { theme: Theme }) => ({
background: theme.palette.grey[50],
}))
推荐阅读
- java - 如何在java中使用枚举键值
- css - 一些 CSS 属性不适用于媒体查询
- python - 登录页面的数据驱动测试用例
- r - 在 R 中使用 parApply 进行并行计算: checkForRemoteErrors(val) 中的错误:7 个节点产生错误;第一个错误:NULL 值作为符号地址传递
- python - PyTorch LSTM 没有在训练中学习
- docker - 如何对每个集装箱化的 celery 工人使用单独的容器服务?
- julia - 有没有办法在 Pluto.jl 中获得暗模式
- qt - 'https' 链接未在 qt creator 的 qwebview 中加载
- javascript - 如何制作具有背景位置和百分比值的可拖动背景图像?
- python - 使用随机的模拟输出差异