Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is possible consume my custom Theme with useTheme() hook?

I have a piece of code when I declare my MuiProvider, like this:

<MuiThemeProvider theme={merge(defaultGlobalTheme, this.props.adjustThemeOptions)}>
    {this.props.children}
</MuiThemeProvider>

How could I consume my custom theme (declared in MuiThemeProvider with @material-ui/[email protected] library) with Material-UI Hooks (declared in @material-ui/[email protected]) or with another solution with Hooks?

EDIT: You can consume with this way:

import { useTheme } from '@material-ui/styles';

function MyComponent() {
  const theme = useTheme();

  return <div>{`spacing ${theme.spacing}`}</div>;
}

BUT my solution works IF you have components with @material-ui/core in the above version and you are using the @material-ui/styles library.

Thank you in advance.

Best, Fran

like image 332
frangaliana Avatar asked Dec 05 '25 16:12

frangaliana


1 Answers

There is no hook to consume the context supplied by the MuiThemeProvider, but if you can use the ThemeProvider instead you can use the useTheme hook to consume the theme in a component that is below the provider in the tree.

import { useTheme } from '@material-ui/styles';

function MyComponent() {
  const theme = useTheme();

  return <div>{`spacing ${theme.spacing}`}</div>;
}
like image 63
Tholle Avatar answered Dec 08 '25 05:12

Tholle