javascript - Material-UI styles ThemeProvider Error: TypeError: Cannot read property 'primary' of undefined - St

I'm trying to include some ponents from @material-ui package at my app, using the ThemeProvider at

I'm trying to include some ponents from @material-ui package at my app, using the ThemeProvider at the root, but i'm having some troubles.

Other ponents that use styles defined locally (without theme properties) render ok.

sandbox:

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 trying to include some ponents from @material-ui package at my app, using the ThemeProvider at the root, but i'm having some troubles.

Other ponents that use styles defined locally (without theme properties) render ok.

sandbox: 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;
Share Improve this question edited Jun 18, 2019 at 21:52 Ryan Cogswell 81.3k9 gold badges241 silver badges212 bronze badges asked Jun 18, 2019 at 21:28 dkotsukadkotsuka 631 gold badge2 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I'm not certain what you were trying to achieve with text: white, but that created an invalid structure for your theme. 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.

Changing AppTheme.js to the following solves the problem:

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;

I've got another error, was fixed here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744215656a4563543.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信