简体   繁体   English

Material-UI样式ThemeProvider错误:TypeError:无法读取未定义的属性“ primary”

[英]Material-UI styles ThemeProvider Error: TypeError: Cannot read property 'primary' of undefined

I'm trying to include some components from @material-ui package at my app, using the ThemeProvider at the root, but i'm having some troubles. 我试图在我的应用程序中包含@ material-ui包中的某些组件,并在根目录使用ThemeProvider,但遇到了一些麻烦。

Other components that use styles defined locally (without theme properties) render ok. 使用本地定义的样式(没有主题属性)的其他组件也可以。

sandbox: https://codesandbox.io/s/theme-provider-issues-t72f3 沙箱: https//codesandbox.io/s/theme-provider-issues-t72f3

Here is how the theme is being created: 这是创建主题的方式:

import React from "react";
import { createMuiTheme } from "@material-ui/core/styles";
import { ThemeProvider } from "@material-ui/styles";
import { blue, white } from "@material-ui/core/colors";

function AppTheme(props) {
  const theme = createMuiTheme({
    palette: {
      primary: blue,
      text: white
    }
  });
  return <ThemeProvider theme={theme}>{props.children}</ThemeProvider>;
}

export default AppTheme;

I'm not certain what you were trying to achieve with text: white , but that created an invalid structure for your theme. 我不确定您要使用text: white实现什么text: white ,但这为您的主题创建了无效的结构。 theme.palette.text should be an object rather than a color, and the error was caused by Material-UI looking for theme.palette.text.primary . theme.palette.text应该是一个对象而不是颜色,并且该错误是由Material-UI寻找theme.palette.text.primary

Changing AppTheme.js to the following solves the problem: 将AppTheme.js更改为以下内容可以解决此问题:

import React from "react";
import { createMuiTheme } from "@material-ui/core/styles";
import { ThemeProvider } from "@material-ui/styles";
import { blue } from "@material-ui/core/colors";

const theme = createMuiTheme({
  palette: {
    primary: blue
  }
});
function AppTheme(props) {
  return <ThemeProvider theme={theme}>{props.children}</ThemeProvider>;
}

export default AppTheme;

编辑主题提供者问题

我还有另一个错误,已在此处修复

暂无
暂无

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

相关问题 使用 material-ui Transitions 时如何修复“TypeError:无法读取未定义的属性“样式”错误 - How to fix 'TypeError: Cannot read property 'style' of undefined' error when using material-ui Transitions ReactJS-&gt; Material-UI未捕获的TypeError:无法读取未定义的属性“ toUpperCase” - ReactJS -> Material-UI Uncaught TypeError: Cannot read property 'toUpperCase' of undefined 什么是 Uncaught TypeError:无法读取 AppBar + Drawer 组件(ReactJS + Material-UI)未定义的属性“打开”? - What is Uncaught TypeError: Cannot read property 'open' of undefined for AppBar + Drawer component (ReactJS + Material-UI)? React Material-UI Modal TypeError:无法读取未定义的属性“hasOwnProperty” - React Material-UI Modal TypeError: Cannot read property 'hasOwnProperty' of undefined 导入错误:在使用 @material-ui/core/styles/ThemeProvider/ 和 @material-ui/core/styles/createMuiTheme/ 时找不到模块 - import error: Module not found on using @material-ui/core/styles/ThemeProvider/ and @material-ui/core/styles/createMuiTheme/ Material-UI:DataGrid 组件错误:无法读取未定义的属性“长度” - Material-UI: DataGrid Component Error: Cannot read property 'length' of undefined 材质UI选项卡-TypeError:无法读取未定义的属性&#39;charAt&#39; - Material UI tabs - TypeError: Cannot read property 'charAt' of undefined TypeError:无法读取未定义的属性“创建”(材料 UI/酶) - TypeError: Cannot read property 'create' of undefined (Material UI/enzyme) Material-UI 上的样式:未定义的 CSS 字段 - Styles on Material-UI: Undefined CSS fields 无法读取未定义的“组件”:在浏览器中使用 material-ui - Cannot read 'component' of undefined: Using material-ui with browserify
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM