skip to Main Content

I’ve followed the tutorial here to add material UI to my app: https://mui.com/material-ui/getting-started

But some components appear not to be styled and customizing the theme doesn’t seem to work either…

I am setting up my theme this way:

// theme.ts
import {createTheme} from '@mui/material/styles';

const ourTheme = createTheme({
    typography: {
        h1: {
            fontSize: '3rem',
            color: 'red',
        },
    },
});

export default ourTheme;

And I wrap my whole app in a ThemeProvider like so:

// App.tsx
import {ThemeProvider} from '@mui/material/styles';
import theme from './theme';
import Home from './pages/Home';

const App: React.FC = () => (
    <ThemeProvider theme={theme}>
        <Home />
    </ThemeProvider>
);

and Home is just a test page defined this way:

// pages/Home.tsx
const Home: React.FC = () => {
    return (
        <div>
            <Typography component='h1'>Home</Typography>
            <p>
                Hello to us, and hello to you!
            </p>
            <p>lala!! :{')'}</p>
            <p>
                <Button variant='contained' color='primary'>
                    This does nothing.
                </Button>
            </p>
        </div>
    );
};

The issue is that the page appears this way:

rendered home page

While I am expecting the h1 to be large and red.

I have tried without my custom theme and the h1 is still unstyled (almost the same as normal text).

Am I missing something obvious here?

2

Answers


  1. Chosen as BEST ANSWER

    I'm stupid, it was variant='h1' and not component='h1', duh...


  2. Try to change your App like this

    // App.tsx
    import {ThemeProvider} from '@mui/material/styles';
    import CssBaseline from '@mui/material/CssBaseline'; // add this
    import theme from './theme';
    import Home from './pages/Home';
    
    const App: React.FC = () => (
        <ThemeProvider theme={theme}>
            <CssBaseline />
            <Home />
        </ThemeProvider>
    );
    

    reference: https://mui.com/material-ui/customization/how-to-customize/#4-global-css-override

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search