【问题标题】:MUI - makeStyles - Cannot read properties of undefinedMUI - makeStyles - 无法读取未定义的属性
【发布时间】:2021-12-01 11:26:03
【问题描述】:

我正在学习 MUI,在课程中,讲师要求我只设计一个组件而不是整个主题。

为此,它使用 ma​​keStyles 函数并传播theme.mixins.toolbar。问题是当我这样做时,出现以下错误:

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

这门课程显然是第 4 版,而我使用的是第 5 版。尽管如此,我的代码似乎遵循了文档要求的更改。那么是什么导致了这个错误呢?

app.js

import "./App.css";
import Header from "./components/ui/Header";
import { ThemeProvider } from "@material-ui/core/styles";
import theme from "./components/ui/Theme";

function App() {
    return (
        <ThemeProvider theme={theme}>
            <Header />
        </ThemeProvider>
    );
}

export default App;

Theme.js

import { createTheme } from "@material-ui/core/styles";

const arcBlue = "#0B72B9";
const arcOrange = "#FFBA60";

export default createTheme({
    typography: {
        h3: {
            fontWeight: 100,
        },
    },
    palette: {
        common: {
            blue: `${arcBlue}`,
            orange: `${arcOrange}`,
        },
        primary: {
            main: `${arcBlue}`,
        },
        secondary: {
            main: `${arcOrange}`,
        },
    },
});

header/index.jsx

import React from "react";
import AppBar from "@mui/material/AppBar";
import Toolbar from "@mui/material/Toolbar";
import useScrollTrigger from "@mui/material/useScrollTrigger";
import Typography from "@mui/material/Typography";
import { makeStyles } from "@material-ui/styles";

function ElevationScroll(props) {
    const { children, window } = props;
    const trigger = useScrollTrigger({
        disableHysteresis: true,
        threshold: 0,
        target: window ? window() : undefined,
    });

    return React.cloneElement(children, {
        elevation: trigger ? 10 : 0,
    });
}

const useStyles = makeStyles((theme) => ({
    toolbarMargin: { ...theme.mixins.toolbar },
}));

function Header() {
    const classes = useStyles();
    return (
        <React.Fragment>
            <ElevationScroll>
                <AppBar color="primary">
                    <Toolbar>
                        <Typography variant="h3" component="h3">
                            Nome de teste
                        </Typography>
                    </Toolbar>
                </AppBar>
            </ElevationScroll>
            <div className={classes.toolBarMargin} />
        </React.Fragment>
    );
}

export default Header;

【问题讨论】:

  • 我有点困惑。你最近不是answer自己吗?

标签: javascript reactjs material-ui jss


【解决方案1】:

由于您使用的是 v5,请将您的 ThemeProvidercreateThememakeStyles 导入路径更改为:

import { ThemeProvider, createTheme, makeStyles } from "@material-ui/core/styles";

收件人:

import { ThemeProvider, createTheme } from "@mui/material/styles";
import { makeStyles } from "@mui/styles";

@material-ui/core 是 v4 包,@mui/material 是 v5 等效包。 2 个版本的 API 不兼容。在 v5 中,makeStyles 也被移动到一个名为 @mui/styles 的旧包中,如果您在新项目中使用 MUI v5,则应完全切换到 styled/sx API as recommended 由 MUI 团队.

相关答案

【讨论】:

  • 除了提到的更改之外,我还需要做三件事。 npm install @mui/styles,然后是 import { makeStyles } from "@mui/styles";````, and I also called the object classes using the correct propety, with b in lowercase. From:
    ``` 到 &lt;div className={classes.toolbarMargin} /&gt;
  • @MaximilianKaden 如果您正在创建一个新项目。您应该放弃makeStyles 的使用并改用styled/sx。这是 v5 中推荐的 API,而 makeStyles 是旧版 API,可能会在未来的 MUI 版本中被删除。
【解决方案2】:

我在 CodeSandbox 上创建了一个项目,代码中似乎没有问题。 我想您需要检查您在package.json 文件中安装的软件包的版本。

这里是 CodeSandbox 项目的链接,您可以在控制台选项卡上看到 console.log 消息。

https://codesandbox.io/s/check-makestyle-eq67m?file=/src/components/ui/Header/index.js

【讨论】:

  • 嗨@jihyun lee。使用 Material UI 的第 4 版确实有效。您的示例代码使用"@material-ui/core" version: "4.12.3"。但更新版本(我正在使用)是"@material-ui/core": "^5.0.0-beta.5"
  • 奥普斯。我的错。应该检查版本。像 NearHuscarl 的回答一样,您需要将模块名称从 @material-ui/core/styles 更改为 @mui/material/styles。我更改了示例代码,它可以工作了!
  • 是的,还需要从 "@mui/styles"; 导入 { makeStyles } 而不是 @material-ui/styles;。顺便说一下,新文档中没有提到这一点。感谢您的帮助!
【解决方案3】:

解决方案

安装@mui/styles

npm install @mui/styles

改变

import { ThemeProvider, createTheme } from "@material-ui/core/styles";

import { ThemeProvider, createTheme } from "@mui/material/styles";

import { makeStyles } from "@material-ui/styles";

import { makeStyles } from "@mui/styles";

&lt;div className={classes.toolBarMargin} /&gt;

&lt;div className={classes.toolbarMargin} /&gt;

【讨论】:

    猜你喜欢
    • 2021-07-11
    • 2022-06-10
    • 1970-01-01
    • 2019-03-21
    • 2021-11-30
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    相关资源
    最近更新 更多