[英]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.