javascript - When Using MUI Button: TypeError: Cannot read property 'borderRadius' of undefined - Stack Overflow

import React, { Component } from 'react';import '.App.css';import Screen from &#

import React, { Component } from 'react';
import './App.css';
import Screen from './ponents/Screen/Screen';
import Button from './ponents/Button/Button';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import pink from '@material-ui/core/colors/pink';

const buttonTheme = createMuiTheme({
  palette: {
    primary: {
      main: '#2dff46',
    },
    secondary: pink,
  }
});


class App extends Component {
  render() {
    return (
      <MuiThemeProvider theme={buttonTheme}>
      <Screen>
        <div>Hello</div>
        <Button variant='contained' color='primary'>
          GO
        </Button>
      </Screen>
      </MuiThemeProvider>
    );
  }
}

export default App;

I am simply trying to create a button with some custom colors (theme). It will work without "theme={buttonTheme}" but of course it uses the default. I get the following error:

TypeError: Cannot read property 'borderRadius' of undefined
styles
node_modules/@material-ui/core/Button/Button.js:41
  38 | minWidth: 64,
  39 | minHeight: 36,
  40 | padding: '8px 16px',
> 41 | borderRadius: theme.shape.borderRadius,
  42 | color: theme.palette.text.primary,
  43 | transition: theme.transitions.create(['background-color', 'box-shadow', 'border'], {
  44 |   duration: theme.transitions.duration.short

thanks!!

import React, { Component } from 'react';
import './App.css';
import Screen from './ponents/Screen/Screen';
import Button from './ponents/Button/Button';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import pink from '@material-ui/core/colors/pink';

const buttonTheme = createMuiTheme({
  palette: {
    primary: {
      main: '#2dff46',
    },
    secondary: pink,
  }
});


class App extends Component {
  render() {
    return (
      <MuiThemeProvider theme={buttonTheme}>
      <Screen>
        <div>Hello</div>
        <Button variant='contained' color='primary'>
          GO
        </Button>
      </Screen>
      </MuiThemeProvider>
    );
  }
}

export default App;

I am simply trying to create a button with some custom colors (theme). It will work without "theme={buttonTheme}" but of course it uses the default. I get the following error:

TypeError: Cannot read property 'borderRadius' of undefined
styles
node_modules/@material-ui/core/Button/Button.js:41
  38 | minWidth: 64,
  39 | minHeight: 36,
  40 | padding: '8px 16px',
> 41 | borderRadius: theme.shape.borderRadius,
  42 | color: theme.palette.text.primary,
  43 | transition: theme.transitions.create(['background-color', 'box-shadow', 'border'], {
  44 |   duration: theme.transitions.duration.short

thanks!!

Share Improve this question asked Sep 26, 2018 at 0:43 NumnumberryNumnumberry 3951 gold badge4 silver badges17 bronze badges 3
  • What does your Button ponent look like? And should this line be import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles'; ? – Patrick Dillon Commented Sep 26, 2018 at 1:35
  • @PatrickDillon YOU ARE CORRECT!!! I got that import from another example. I guess it is outdated or something. Thanks for the response. – Numnumberry Commented Sep 26, 2018 at 4:14
  • @PatrickDillon.. You're my SAVIOR ..spent 3 hours to fix this...BOTTOMS UP.. – muhnizar Commented Oct 14, 2018 at 11:24
Add a ment  | 

3 Answers 3

Reset to default 5

As mentioned in an earlier ment, the import statement was incorrect. This:

import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';

Should be this:

import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';

In case anybody else has a similar issue. The above answer never fixed my problem but pointed me in the correct direction I had to add

shape: {
 borderRadius: 16
}

To my material ui theme.

So this is a two step thing for you, I'm not across Material-ui, but the main issue is that the theme-shape isn't being provided to your button ponent.

The first thing i'd do is debug and log out the buttonTheme constant to confirm that it is matching the theme defined in https://material-ui./customization/default-theme/ with the addition of your overrides.

If you can see the the shape: border-radius: 4 portion then you know it is an issue with MuiProvider, but from looking at your code it seems to be correct.

Let me know what the theme looks like (Update your question) and we can work from there

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信