Search code examples
reactjsmaterial-uistyled-components

How can I use useTheme in Material UI 5?


I just started using Material UI 5.0.4 (with styled-components), and I wanted to access the theme in a component. I looked online and saw useTheme, so I checked the docs and found it - @mui/styles/useTheme. However, it was the legacy documentation, and @mui/styles does not exist in MUI 5. So, I looked at @mui/system instead, and found the section "Accessing the theme in a component". However, this just points back to the legacy documentation!

After the docs didn't seem to help me, I decided to use Visual Studio Code's "Quick Fix" feature, where if you hover over the function, VSCode will give you a list of options to import. Here is the list of options I tried, and why they didn't work:

  • @mui/material/styles/useTheme - Returns the default theme object, no matter what. Looking into the source code, this is literally what it does - it switches to the default theme, and then returns the theme.
  • @mui/material/private-theming/useTheme - This just returns null. I feel like I shouldn't be accessing this anyway (it says private-), but I tried it anyway.
  • @mui/system/useTheme - This is what I was hoping would work. However, this is also probably the weirdest one. It gives me the default theme, but it excludes many properties. For example, it only provided palette.mode, and there are no other keys under palette than that. (You can see the whole thing below)
{
    "breakpoints": {
        "keys": ["xs", "sm", "md", "lg", "xl"],
        "values": { "xs": 0, "sm": 600, "md": 900, "lg": 1200, "xl": 1536 },
        "unit": "px"
    },
    "direction": "ltr",
    "components": {},
    "palette": { "mode": "light" },
    "shape": { "borderRadius": 4 }
}
  • styled-components/useTheme - Returns undefined.
  • @mui/styled-engine-sc/useTheme - Returns undefined. (I have a feeling this is the same thing as styled-components/useTheme.)

Those were all the suggestions that VSCode could give me, apart from things like @mui/system/useTheme vs @mui/system/useTheme/useTheme (which is the same thing). I also tried googling stuff but it would always be really old, like:

Please, if someone knows, how do you get the theme in a component in MUI 5?


Solution

  • It turns out that the correct useTheme is @mui/material/styles/useTheme, and you cannot use useTheme in the same component that you do the ThemeProvider in. For example, this:

    const App = () => {
        const theme = useTheme();
        return (
            <ThemeProvider theme={myTheme}>
                <Box bgcolor={theme.palette.background.default} width={100} height={100} />
            </ThemeProvider>
        );
    };
    

    Will not work properly. However, this:

    const MyComponent = () => {
        const theme = useTheme();
        return <Box bgcolor={theme.palette.background.default} width={100} height={100} />;
    };
    
    const App = () => (
        <ThemeProvider theme={myTheme}>
            <MyComponent />
        </ThemeProvider>
    )
    

    Will work properly, as useTheme is used in a separate component.