材质用户界面:无法自定义主题

时间:2018-06-01 04:39:32

标签: reactjs material-ui

我正在尝试自定义我的应用程序的主题(使用Material UI):

https://material-ui.com/customization/themes/#muithemeprovider

的src / index.js:

import App from './App';
import MuiThemeProvider from '@material-ui/core/styles/MuiThemeProvider';
import { createMuiTheme } from '@material-ui/core/styles';
import red from '@material-ui/core/colors/red';
import purple from '@material-ui/core/colors/purple';
import green from '@material-ui/core/colors/green';

const theme = createMuiTheme({
  palette: {
    primary: green,
    secondary: green,
  },
  status: {
    danger: 'orange',
  },
});

ReactDOM.render(
  <MuiThemeProvider muiTheme={theme}>
    <App />
   </MuiThemeProvider>
  , document.getElementById('root'));

的src / App.js:

import { withTheme } from '@material-ui/core/styles'
class App extends Component {
   ...
}

export default withTheme()(App);

但是,默认主题颜色仍然显示给我: enter image description here

我尝试从Firebase控制台实现此颜色主题: enter image description here 为什么不起作用?

2 个答案:

答案 0 :(得分:5)

这一行:

  <MuiThemeProvider muiTheme={theme}>

需要:

  <MuiThemeProvider theme={theme}>

答案 1 :(得分:0)

我认为你错过了withTheme() https://material-ui.com/customization/themes/#withtheme-component-component

Root组件中,您应该导出:

import { withTheme } from '@material-ui/core/styles';

export default withTheme()(Root);