简体   繁体   English

使用 React 时如何在 Material UI 中访问主题 object?

[英]How do I access the theme object in Material UI when using React?

There was a recent upgrade in Material UI (@mui) and I'm having trouble accessing the theme object (see below code). Material UI (@mui) 最近进行了升级,我无法访问主题 object(请参见下面的代码)。 The below is incredibly easy code, but it's not working.下面是非常简单的代码,但它不起作用。 After 5 hours on this, I'm hoping someone can help me pinpoint the issue. 5个小时后,我希望有人能帮我查明问题。

The error I get is that React can't access "theme" so the property theme.palette.common.black can't be read.我得到的错误是 React 无法访问“主题”,因此无法读取属性 theme.palette.common.black。

import React from "react";

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

const useStyles = makeStyles((theme) =>
  createStyles({
    monster: {
      backgroundColor: theme.palette.common.black,
    },
  })
);

const theme = createTheme();

function Header(props) {
  const classes = useStyles();

  return (
    <ThemeProvider theme={theme}>
      <Typography variant="h1" className={classes.monster}>
        Hi
      </Typography>
    </ThemeProvider>
  );
}

export default Header;

The example in the documentation is this:文档中的示例是这样的:

import React from "react";

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

const theme = createTheme();

const useStyles = makeStyles((theme) => ({
  root: {
    color: theme.palette.primary.main,
  },
}));

const Header = (props) => {
  const classes = useStyles();
  return (
    <ThemeProvider theme={theme}>
      <div {...props} className={classes.root}>
        Hello
      </div>
    </ThemeProvider>
  );
};

export default Header;

But when I run this code, I get this error message:但是当我运行此代码时,我收到此错误消息:

TypeError: Cannot read properties of undefined (reading 'primary')
(anonymous function)
src/components/Header/Header.js:13
  10 | 
  11 | const useStyles = makeStyles((theme) => ({
  12 |   root: {
> 13 |     color: theme.palette.primary.main,
  14 |   },
  15 | }));
  16 | 

You should defined type file that specify the theme in makeStyles which has DefaultTheme interface extends from Theme interface.您应该在具有 DefaultTheme 接口的makeStyles中定义指定主题的类型文件,该接口扩展自 Theme 接口。

For example is TS project you can:例如是 TS 项目,您可以:


import { Theme } from '@mui/material/styles';

declare module '@mui/styles/defaultTheme' {
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
  interface DefaultTheme extends Theme {}
}


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

相关问题 如何使用 React 和 Material UI 访问列表中所选项目的索引? - How do I access the index of a selected item in list using React and Material UI? 我如何在 React 中使用 Material UI 进行 Inset FAB? - How can i do Inset FAB using Material UI in React? 如何在React中自定义Material UI? - How do I customize Material UI in React? 如果我使用的是 Material Ui,我如何在 React 中获取输入字段的值? - How do I get value of input field in React, if I am using Material Ui? 当为 Material UI Autocomplete React 组件选择任何选项时,如何获得事件? - How do I get an event when any option is selected for a Material UI Autocomplete React component? 如何导出具有 2 个样式对象的 react material-ui 组件? (材质-ui V1) - How can I export a react material-ui component with 2 styles object? (material-ui V1) 如何使用 React 在对话框 (material-ui) 中传递组件? - How do I pass in a component in a dialogue box (material-ui), using React? 如何使用 React 的 Material UI 创建响应式侧边栏组件? - How do I create a responsive sidebar component using React's Material UI? 如何在 React (Material-UI) 中切换 DOM 类? - How do I toggle DOM classes in React (Material-UI)? 我如何在 Material-ui 中实现下拉标题以反应? - How do i implement a dropdown header in Material-ui in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM