TypeError:theme.spacing 不是一个函数
2021-10-13
3659
我正在开发 React 应用并使用 Material-ui v5,但遇到了这个错误 theme.spacing 不起作用。
import { makeStyles } from "@material-ui/styles";
import React from "react";
import Drawer from "@mui/material/Drawer";
import Typography from "@mui/material/Typography";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemIcon from "@mui/material/ListItemIcon";
import ListItemText from "@mui/material/ListItemText";
import { AddCircleOutlineOutlined, SubjectOutlined } from "@mui/icons-material";
import { useHistory, useLocation } from "react-router";
const drawerWidth = 240;
const useStyles = makeStyles((theme) => {
return {
page: {
background: "#f9f9f9",
width: "100%",
padding: theme.spacing(3),
},
drawer: {
width: drawerWidth,
},
drawerPaper: {
width: drawerWidth,
},
root: {
display: "flex",
},
active: {
background: "#f4f4f4 !important",
},
};
});
const Layout = ({ children }) => {
const classes = useStyles();
const history = useHistory();
const location = useLocation();
const menuItems = [
{
text: "My Notes",
icon: <SubjectOutlined color="secondary" />,
path: "/",
},
{
text: "Create Note",
icon: <AddCircleOutlineOutlined color="secondary" />,
path: "/create",
},
];
return (
<div className={classes.root}>
<div className={classes.page}>{children}</div>
</div>
);
};
export default Layout;
第 19 行“padding: theme.spacing(3)”在我的浏览器中显示错误
TypeError: theme.spacing is not a function
3个回答
此方法无效,因为您必须输入主题
-
导入主题
从 '@mui/material' 导入 { Theme }
-
现在输入:
const useStyles=makeStyles((theme:Theme)=>({ root:{ background: 'black', padding:theme.spacing(3), } }))
我遇到了完全相同的问题,此方法对我有效。
Ellson Mendes
2021-12-14
您如何创建主题,您是用 createTheme 创建的吗?您需要在应用程序的根目录中传递提供程序。尝试像这样导入。
import { createTheme } from '@mui/material/styles
然后为您的应用程序定义所有全局样式,然后创建,例如,这个。
import { ThemeProvider } from "@mui/material/styles";
<ThemeProvider theme={yourtheme}><App/></ThemeProvider>
Tolme
2021-10-28
也许你的导入有误?试试这个
import {makeStyles} from "@material-ui/core/styles";
Manfre
2021-10-13