MUI CreateTheme Is Not Properly Passing Theme To MUI Components
I have created a theme in the index of my React.JS project using MUI. When I try to apply my style to my Appbar the theme does not correctly modify the menu button nor the menu its
Solution 1:
Change this line:
import { ThemeProvider } from "@mui/styles";
To:
import { ThemeProvider } from "@mui/material/styles";
Reason: There are 2 ThemeProviders here
- The one from
@mui/styles: ThisThemeProviderdoes send theThemeobject down via context, it works fine, you can still access it using theuseThemehook:
const theme = useTheme();
return <Box sx={{ width: 10, height: 10, bgcolor: theme.palette.primary.main }} />
- The one from
@mui/material/styles: ThisThemeProvideris a wrapper of the above, but it also injects the theme to theStyledEngineThemeContext.Provider, which allows you to access the theme when using MUI API (sxprop/styled()). The problem here is that theButtonandMenucomponents uses thestyled()API under-the-hood so theThemeProviderneeds to be imported from@mui/material/stylesto make it work.
return <Box sx={{ width: 10, height: 10, bgcolor: 'primary.main' }} />

Post a Comment for "MUI CreateTheme Is Not Properly Passing Theme To MUI Components"