【问题标题】:MUI breakpoints not recognizing "theme.breakpoints.down"MUI 断点无法识别“theme.breakpoints.down”
【发布时间】:2021-11-13 01:42:18
【问题描述】:

目标:当断点为平板电脑及以下时隐藏 navMenu,以便我可以用汉堡菜单替换

在 VS 代码的终端中,它说编译成功,但在浏览器中我看到:

TypeError: Cannot read properties of undefined (reading 'down')

我在这里尝试了说明:StackOverflow Question,但没有运气。

有人能指出正确的方向吗?

import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Toolbar from "@mui/material/Toolbar";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import { makeStyles } from "@mui/styles";

const useStyles = makeStyles((theme) => ({
  navMenu: {
    [theme.breakpoints.down('md')]: {
      display: "none",
    },
  },
}));

const Navbar = () => {
  const classes = useStyles();

  return (
    <Box sx={{ flexGrow: 1 }}>
      <AppBar position="static" style={{ backgroundColor: "#061B2E" }}>
        <Toolbar>
          <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
            Name
          </Typography>
          <Box className={classes.navMenu}>
            <Button color="inherit">Item 1</Button>
            <Button color="inherit">Item 2</Button>
            <Button color="inherit">Item 3</Button>
            <Button color="inherit">Item 4</Button>
          </Box>
        </Toolbar>
      </AppBar>
    </Box>
  );
};

export default Navbar;

【问题讨论】:

标签: javascript html css reactjs material-ui


【解决方案1】:

很好的问题,您使用的是哪个版本的 MUI?他们有点从makeSyles 转向样式化组件,但仍然支持这种方法(我们仍然只在我的团队中使用它)。您可能需要将您的导入语句更改为import { makeStyles } from '@material-ui/core';

【讨论】:

  • 我正在使用“@mui/material”:“^5.1.0”。如果有更好的方法来解决这个问题,我将如何让这对初学者更友好?即使是资源链接也会有很大帮助
  • 如果您使用npm install @material-ui/core 安装核心包,然后将您的导入语句更改为上面提到的内容,它应该可以按照您的方式工作。
  • 从 mui 开始的一个令人困惑的事情是不同的包。大多数来自@mui/material,但偶尔你会使用来自@mui/system@material-ui/core的一些
  • 或者,如果您想在 mui 中使用更新的样式引擎,这部分文档可能会有所帮助 mui.com/system/styled
  • 我也看看,非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 2015-12-24
  • 2021-09-06
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多