简体   繁体   English

MUI - makeStyles - 无法读取未定义的属性

[英]MUI - makeStyles - Cannot read properties of undefined

I'm studying MUI, and in the course, the instructor asks me to style just one component and not the entire theme.我正在学习 MUI,在课程中,讲师要求我只设计一个组件而不是整个主题。

For that, it uses the makeStyles function and spreads the theme.mixins.toolbar .为此,它使用makeStyles function 并传播theme.mixins.toolbar The problem is when I do this, I have the following error:问题是当我这样做时,出现以下错误:

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

This course is apparently in version 4, and I am using version 5. Despite this, my code appears to follow the changes that the documentations asks for.这门课程显然是第 4 版,而我使用的是第 5 版。尽管如此,我的代码似乎遵循了文档要求的更改。 So what could be causing this error?那么是什么导致了这个错误呢?

app.js应用程序.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主题.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标头/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;

I created a project on CodeSandbox and it doesn't seem the problem in code.我在 CodeSandbox 上创建了一个项目,代码中似乎没有问题。 I guess you need to check the version of package you installed in package.json file.我猜你需要检查你在package.json文件中安装的package.json的版本。

Here is the link to the CodeSandbox project and you can see the console.log message on console tab.这是 CodeSandbox 项目的链接,您可以在控制台选项卡上看到 console.log 消息。

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

Since you're using v5, change your ThemeProvider , createTheme and makeStyles import path from:由于您使用的是 v5,因此请更改您的ThemeProvidercreateThememakeStyles导入路径:

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

To:到:

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

@material-ui/core is v4 package and @mui/material is the v5 equivalent. @material-ui/core是 v4 包, @mui/material是 v5 等价的。 The API from the 2 versions are not compatible.两个版本的 API 不兼容。 In v5, makeStyles is also moved to a legacy package called @mui/styles , if you are using MUI v5 in a new project, you should switch completely to styled / sx API as recommended by the MUI team.在 v5 中, makeStyles也被移到了一个名为@mui/styles的遗留包,如果你在新项目中使用 MUI v5,你应该按照 MUI 团队的建议完全切换到styled / sx API。

Related answers相关回答

Solution解决方案

Install @mui/styles安装@mui/styles

npm install @mui/styles

Change改变

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

To

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

And

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

To

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

And

<div className={classes.toolBarMargin} />

To

<div className={classes.toolbarMargin} />

I faced similar issue with Can not read properties of undefined, reading refs thrown by makeStyles .我遇到了类似的问题Can not read properties of undefined, reading refs抛出makeStyles In my case it was mui v5 and the issue occurred after upgrading to React 18.在我的情况下,它是 mui v5,并且问题发生在升级到 React 18 之后。

In my case it turned out I had some legacy makeStyles(()=>createStyles) wrapped in a manner:就我而言,事实证明我有一些遗留makeStyles(()=>createStyles)以某种方式包装:

const useStyles = (minWidth: number) =>
  makeStyles((theme: Theme) => ...

I had to change it to:我不得不将其更改为:

const useStyles =
  makeStyles((theme: Theme) => ...

Perhaps it will be useful for somebody else facing the issue.也许它对面临这个问题的其他人有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MUI 5 无法读取未定义的属性(读取“向上”),使用 makestyles 时 - MUI 5 Cannot read properties of undefined (reading 'up'), When using makestyles (MUI 文本字段)未捕获的 TypeError:无法读取未定义的属性(读取“焦点”) - (MUI Textfield) Uncaught TypeError: Cannot read properties of undefined (reading 'focus') MUI 警报未捕获类型错误:无法读取未定义的属性(读取“光”) - MUI Alert Uncaught TypeError: Cannot read properties of undefined (reading 'light') mui 数据表无法读取未定义的属性(读取“下载”) - mui datatable Cannot read properties of undefined (reading 'download') MUI 错误:无法读取未定义的属性(读取“抽屉”) - MUI error: Cannot read properties of undefined (reading 'drawer') 从 mui v4 迁移到 v5 | TypeError:无法读取未定义的属性(读取“keyboardDate”) - Migrating from mui v4 to v5 | TypeError: Cannot read properties of undefined (reading 'keyboardDate') MUI-Datatable:显示数据显示此错误 Cannot read properties of undefined (reading 'split') - MUI-Datatable: Displaying the data shows this error Cannot read properties of undefined (reading 'split') React MUI 4 SelectInput.js:340 Uncaught TypeError: 无法读取未定义的属性(读取“值”) - React MUI 4 SelectInput.js:340 Uncaught TypeError: Cannot read properties of undefined (reading 'value') 无法将属性读取为未定义 - Cannot read properties as undefined 无法读取未定义的属性(读取“打开”) - Cannot read properties of undefined (reading 'on')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM