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 working from @mui/material/styles, but does from @mui/styles.

See original GitHub issue

Hi,

I’m trying to port a moderately sized project from MaterialUI4 to MUI5. I’m following the migration instructions but have hit a bit of a wall with the following;

I am using the createStyles/makeStyles APIs from @mui/styles, however I get the following error when running unit tests or launching the project;

      MUI: The `styles` argument provided is invalid.
      You are providing a function without a theme in the context.
      One of the parent elements needs to use a ThemeProvider.

My tests are all wrapped in a <ThemeProvider> instance.

My initial thoughts were I am somehow including multiple copies of React or another library which is causing this issue, however this does not appear to be the case.

By trial and error, I’ve found that changing the ThemeProvider import from import { ThemeProvider } from "@mui/material/styles"; to import { ThemeProvider } from "@mui/styles";

Allows the tests to pass, and the project to launch, but this doesn’t look right, as the migration guide states;

_https://mui.com/guides/migration-v4/#mui-styles_

If you are using the utilities from @mui/styles together with the @mui/material, you should replace the use
of ThemeProvider from @mui/styles with the one exported from @mui/material/styles. This way, the theme
provided in the context will be available in both the styling utilities exported from @mui/styles, like
makeStyles, withStyles etc. and the MUI components.

So before I start going through fixing the styles etc. Does anyone have any idea why I might be seeing this? Is this something that has been encountered before?

The only possible suspicious thing I’ve found is that running npm ls @mui/system

Results in the following;

my.project@0.1.0 C:\project_path\ClientApp
├─┬ @mui/lab@5.0.0-alpha.59
│ └── @mui/system@5.2.3
└─┬ @mui/material@5.2.3
  └── @mui/system@5.2.3

Why would there be two copies of the @mui/system package? Notably this is where ThemeProvider lives so I see this as a red flag. Shouldn’t these be deduped? The same thing happens with @mui/base.

By including @mui/system and @mui/base directly into my package.json this creates a deduping effect;

my.project@0.1.0 C:\project_path\ClientApp
├─┬ @mui/lab@5.0.0-alpha.59
│ └── @mui/system@5.2.3 deduped
├─┬ @mui/material@5.2.3
│ └── @mui/system@5.2.3 deduped
└── @mui/system@5.2.3

The issue however, persists.

Node: 16.13.0 Npm: 8.1.0 React Scripts: 4.3.0

any help greatly appreciated!

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
TheBox193commented, Mar 22, 2022

Had a similar issue. It surfaced with useStyle not having my theme and was an empty object.

I solved by adding two <ThemProvider /> for a little bit.

import { ThemeProvider } from "@mui/material";
import { ThemeProvider as ThemeProviderLegacy } from "@mui/styles";

<ThemeProvider theme={theme}>
  <ThemeProviderLegacy theme={theme}>
      {children}
  </ThemeProviderLegacy>
</ThemeProvider>

However, I realize this was silly.

I opened my yarn.lock and saw I had two versions of @mui/private-theming. The fix was to make it resolve to 1 version. More specifically I also made sure all my @mui/* were de-duped. I did so by removing all @mui/* in yarn.lock and running yarn install. Nice and easy 👍🏻

0reactions
manuellysuzikcommented, Sep 20, 2022

The easiest way that I founded was using useTheme with themeProvider from @mui/material.

Here’s the example CodeExample This goes in my app.js ( on react-app ). This way you can call a compatible defaultTheme on your makeStyles 😄

I’m using v5.10 of mui

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is ThemeProvider (Material UI) not working for me here?
In MUI V5 you need to import ThemeProvider and createTheme from @mui/material/styles instead of @mui/styles . import * as React from 'react' ...
Read more >
Troubleshooting - Material UI - MUI
This document covers known issues and common problems encountered when migrating ... createTheme } from '@mui/material/styles'; import { ThemeProvider as ...
Read more >
Why is ThemeProvider (Material UI) not working for me here?
In MUI V5 you need to import ThemeProvider and createTheme from @mui/material/styles instead of @mui/styles . import * as React from 'react'; ...
Read more >
Theming with React and MUI - Welcome, Developer
The goal of this post is not to dive into the details of Material-UI, but ... Cool, let's run the app and make...
Read more >
The Ultimate Guide to Material UI Theme Breakpoints
In this tutorial we will customize theme breakpoints and show an ... such as import { ThemeProvider } from "@mui/styles"; not working.
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