reactjs - React MUI V5 AppBar背景颜色不变
问题描述
import React from "react";
import {
AppBar,
Toolbar,
Grid,
IconButton,
InputBase,
Badge,
} from "@mui/material";
import {
ChatBubbleOutline,
NotificationsNone,
PowerSettingsNew,
} from "@mui/icons-material";
import { makeStyles } from "@mui/styles";
const useStyles = makeStyles({
root: {
backgroundColor: "#fff",
},
});
const Header = () => {
const classes = useStyles();
return (
<AppBar position="static" className={classes.root}>
<Toolbar>
<Grid container>
<Grid item>
<InputBase />
</Grid>
<Grid item sm>
helo
</Grid>
<Grid item>
<IconButton>
<Badge badgeContent={4} color="secondary">
<NotificationsNone />
</Badge>
</IconButton>
<IconButton>
<Badge badgeContent={3} color="primary">
<ChatBubbleOutline />
</Badge>
</IconButton>
<IconButton>
<PowerSettingsNew />
</IconButton>
</Grid>
</Grid>
</Toolbar>
</AppBar>
);
};
export default Header;
我正在使用“makeStyles”进行样式设置,但它似乎无法更改 appbar 的背景颜色。应用栏是来自 v5(最新版本)的 mui 组件。我不知道为什么它没有改变。我是 MUI 的新手。有人请帮助我。
我可以使用内联“样式”及其以这种方式工作。但我需要使用 makeStyles 方法。
注意:我使用的是最新版本的 mui,但我不知道为什么它没有改变。
解决方案
这是最新的 MUI v5。
问题是@mui/styles与React.StrictMode 或 React 18不兼容。 因此,我们需要将我们的根“App”组件包装在 index.js 文件中,并使用“StyledEngineProvider”并将 injectFirst 设置为 True。
import { StyledEngineProvider } from "@mui/material/styles";
ReactDOM.render(
<StyledEngineProvider injectFirst>
<App />
</StyledEngineProvider>,
document.getElementById("root")
);
然后它将起作用。
推荐阅读
- python - Boost.Python:匹配 C++ 模板类型的嵌套命名空间
- android - 如何恢复丢失的官方 Android SDK 更新站点?
- python-3.x - 除了在 Python 或其他编程语言中进行正面、负面和中性之外,是否可以进行情绪分析
- android - 导航抽屉活动的 MainActivity 中不存在 onNavigationItemSelected() 函数
- android - 以编程方式在谷歌地图android中设置从位置到当前位置
- google-sheets - 如何将单元格引用到文本字符串中
- java - Spring Data 不记录 findById
- python - 类实例存储与最初给定不同的值
- excel - 将特定单元格相加到某个值,然后将剩余的单元格相乘
- f# - F# 中的引号转换为另一种类型