question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ThemeProvider not providing theme to consumer in 5.2.1 +

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Consuming theme in child class of ThemeProvider produces the following error:

TypeError: theme.spacing is not a function marginTop: theme.spacing(8)

or

TypeError: Cannot read properties of undefined (reading ‘secondary’) backgroundColor: theme.palette.secondary.main

Expected behavior 🤔

In 5.2.0, ThemeProvider provides a theme that executes both of these properly.

Steps to reproduce 🕹

Steps:

  1. run create-react-app
  2. App.js:
import React from 'react';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { StyledEngineProvider } from '@mui/styled-engine';

import Example from './pages/Example';

const theme = createTheme();

function App() {
  return (
    <div>
      <ThemeProvider theme={theme}>
        <StyledEngineProvider injectFirst>
          <Example/>
        </StyledEngineProvider>
      </ThemeProvider>
    </div>
  );
}
export default App;
  1. pages/Example.js:
import React, { useState } from 'react';

// Material UI Components
import { 
    Avatar,
    Button,
    CircularProgress,
    Container,
    TextField,
    Link,
    Grid,
    Typography } from '@mui/material';
import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
import { withStyles } from '@mui/styles';

import axios from 'axios';

const styles = (theme) => ({
	avatar: {
        backgroundColor: theme.palette.secondary.main,
		margin: theme.spacing(1)
	},
    paper: {
		marginTop: theme.spacing(8),
		display: 'flex',
		flexDirection: 'column',
		alignItems: 'center'
	}
});

const Example = (props) => {
    const { classes } = props;
    
    return (
        <Container component="main" maxWidth="xs">
            <div className={classes.paper}>
                <Avatar className={classes.avatar}>
                    <LockOutlinedIcon />
                </Avatar>
                <Typography component="h1" variant="h5">
                    Login
                </Typography>
                <form className={classes.form} noValidate>
                    <TextField
                        id="email"
                        name="email"
                        label="Email Address"
                        variant="outlined"
                        margin="normal"
                        autoFocus
                        required
                        fullWidth
                    />
                    <TextField
                        id="password"
                        name="password"
                        label="Password"
                        variant="outlined"
                        margin="normal"
                        type="password"
                        required
                        fullWidth
                    />
                </form>
            </div>
        </Container>
    );
}

export default withStyles(styles)(Example);
  1. Run npm start.

Context 🔦

I am attempting to consume a theme from a parent ThemeProvider in a React functional component. This worked as of 5.2.0, but no longer works as of 5.2.1.

Your environment 🌎

Browser: Chrome System: OS: Windows 10 10.0.19042 Binaries: Node: 14.16.0 - C:\Program Files\nodejs\node.EXE Yarn: Not Found npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: Not Found Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.34) npmPackages: @emotion/react: ^11.6.0 => 11.6.0 @emotion/styled: ^11.6.0 => 11.6.0 @mui/base: 5.0.0-alpha.56 @mui/icons-material: ^5.2.0 => 5.2.0 @mui/material: ^5.2.0 => 5.2.0 @mui/private-theming: 5.2.0 @mui/styled-engine: 5.2.0 @mui/styles: ^5.2.1 => 5.2.1 @mui/system: 5.2.0 @mui/types: 7.1.0 @mui/utils: 5.2.0 @types/react: 17.0.36 react: ^17.0.2 => 17.0.2 react-dom: ^17.0.2 => 17.0.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mnajdovacommented, Nov 30, 2021

It is already included in the migration guide - https://mui.com/guides/migration-v4/#mui-styles

2reactions
mnajdovacommented, Nov 30, 2021

This is a type error, you need to use module augmentation and define the DefaultTheme, with something like this:

import { Theme } from '@mui/material/styles';

declare module '@mui/styles' {
  interface DefaultTheme extends Theme {}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is ThemeProvider (Material UI) not working for me here?
I want my H2 tag in the nested component to use Arial. However, it's rendering using Times. I'm not sure why. Here's my...
Read more >
Mui 5 theme overrides - Grande Pelle
Oct 12, 2021 · Using MUI 5, I'm looking to use ThemeProvider and createTheme to set the ... The theme provides a set...
Read more >
cannot read properties of undefined (reading 'consumer')
I am attempting to consume a theme from a parent ThemeProvider in a React functional component. This worked as of 5.2.0, but no...
Read more >
Theme Package - Paste: The Design System for building ...
The Paste Theme Package allows easy and simple access to the design tokens via a theme provider to React components.
Read more >
Advanced Usage - styled-components
styled-components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found