MUI createTheme没有正确地将主题传递给MUI组件
我已经使用MUI在我的React.JS项目的索引中创建了一个主题。当我尝试将我的样式应用于Appbar
时,主题不能正确修改菜单按钮或菜单本身。该按钮看起来像一般的默认按钮,当它应该与Appbar
本身的颜色匹配时,菜单将保持白色。
我的index.tsx如下所示:
import React from "react";
import ReactDOM from "react-dom";
import AppbarTop from "./AppbarTop";
import { Router } from "react-router";
import { createBrowserHistory } from "history";
import AdapterDateFns from "@mui/lab/AdapterDateFns";
import { LocalizationProvider } from "@mui/lab";
import { createTheme } from "@mui/material";
import { ThemeProvider } from "@mui/styles";
import { StyledEngineProvider } from "@mui/material/styles";
const customHistory = createBrowserHistory();
const theme = createTheme({
palette: {
primary: {
main: "#242526"
},
secondary: {
main: "#d975d0"
},
text: {
primary: "#E4E6EB",
secondary: "#B0B3B8"
},
background: {
default: "#242526",
paper: "#242526"
}
}
});
ReactDOM.render(
<React.StrictMode>
<LocalizationProvider dateAdapter={AdapterDateFns}>
<Router history={customHistory}>
<ThemeProvider theme={theme}>
<StyledEngineProvider injectFirst>
<AppbarTop />
</StyledEngineProvider>
</ThemeProvider>
</Router>
</LocalizationProvider>
</React.StrictMode>,
document.getElementById("root")
);
我的appbar.tsx如下所示:
import React from "react";
import {
AppBar,
Box,
Button,
Container,
Menu,
MenuItem,
Toolbar
} from "@mui/material";
import HomeIcon from "@mui/icons-material/Home";
import { makeStyles } from "@mui/styles";
const useStyles = makeStyles((theme?: any) => ({
appBar: {
background: theme.palette.primary.main,
height: "60px",
position: "relative"
}
}));
const AppbarTop: React.FC<{ [key: string]: any }> = () => {
const classes = useStyles();
const [anchorEl, setAnchorEl] = React.useState<any>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
return (
<>
<AppBar position="static" className={classes.appBar}>
<Toolbar>
<Button
id="basic-button"
aria-controls="basic-menu"
aria-haspopup="true"
aria-expanded={open ? "true" : undefined}
onClick={handleClick}
>
Dashboard
</Button>
<Menu
id="basic-menu"
anchorEl={anchorEl}
open={open}
onClose={handleClose}
MenuListProps={{
"aria-labelledby": "basic-button"
}}
>
<MenuItem onClick={handleClose}>
<HomeIcon />{" "}
</MenuItem>
</Menu>
{/*test speed dial*/}
<Container maxWidth="sm"></Container>
<Box></Box>
</Toolbar>
</AppBar>
</>
);
};
export default AppbarTop;
有人能解释一下我错过了什么吗?
解决方案
更改此行:
import { ThemeProvider } from "@mui/styles";
收件人:
import { ThemeProvider } from "@mui/material/styles";
原因:此处有2个ThemeProvider
- The one from
@mui/styles
:这ThemeProvider
确实通过上下文向下发送了Theme
对象,它工作得很好,您仍然可以使用useTheme
钩子访问它:
const theme = useTheme();
return <Box sx={{ width: 10, height: 10, bgcolor: theme.palette.primary.main }} />
@mui/material/styles
中的ThemeProvider
:这个ThemeProvider
是上面的一个包装器,但是它也将主题注入到StyledEngineThemeContext.Provider
中,允许您在使用MUI API(sx
prop/styled()
)时访问主题。这里的问题是,Button
和Menu
组件uses是styled()
底层API,因此需要从@mui/material/styles
导入ThemeProvider
才能正常工作。
return <Box sx={{ width: 10, height: 10, bgcolor: 'primary.main' }} />
相关答案
- Difference between @mui/material/styles and @mui/styles?
- Cannot use palette colors from MUI theme
- MUI - makeStyles - Cannot read properties of undefined
- Material UI Dark Mode
相关文章