React JS: Apply Material-UI CssBaseline

僤鯓⒐⒋嵵緔 提交于 2019-12-04 05:53:10

The CSSBaseline component should be used inside a Material UI ThemeProvider component. In your example you did not include a ThemeProvider so there is no Material UI theme.

See official documentation for how to setup ThemeProvider : https://material-ui.com/customization/themes/#muithemeprovider

Based on this sample, a minimal working example with CSSBaseline would be :

import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import CssBaseline from "@material-ui/core/CssBaseline";
import Root from './Root';

const theme = createMuiTheme();

function App() {
  return (
    <MuiThemeProvider theme={theme}>
      <React.Fragment>
        <CssBaseline />
        <Root />
      </React.Fragment>
    </MuiThemeProvider>
  );
}

render(<App />, document.querySelector('#app'));

To get the Roboto font loaded, add this to your html template

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">

For a more complete sample, look at the source code of this official sample : https://github.com/mui-org/material-ui/tree/master/examples/create-react-app/src

  • public/index.html : loading of roboto font
  • src/withRoot.js : ThemeProvider and CSSBaseline setup
  • src/pages/index.js : sample components with MUI theme applied
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!