Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom theme in makeStyles

I have done some research and was not able to find a solution.

I am trying to create a theme using createMuiTheme and use it later in makeStyles to retrieve the keys inside. (palette in the example) However it seems that the custom theme does not get applied in makeStyles.

import React from 'react';
import ReactDOM from 'react-dom';
import { makeStyles, createMuiTheme, ThemeProvider } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import red from "@material-ui/core/colors/red";

const useStyles = makeStyles(theme => {
  console.log(theme); // Only defaults but not red in palette.
  return {
    root: {
      color: theme.palette.primary[400] // This is not working.
    }
  };
});

function App() {
  const classes = useStyles();
  const theme = createMuiTheme({
    palette: {
      primary: red
    }
  });
  return (
    <ThemeProvider theme={theme}>
      <Button className={classes.root}>Hook</Button>
    </ThemeProvider>
  );
}

However in another component using useTheme it works without any issue. What have I done wrong?

const useStyles = makeStyles(theme => {
  console.log(theme); // Here palette includes the new palette.
  return {
    root: {
      color: theme.palette.primary[400]
    }
  };
});

function anotherApp() {
  const classes = useStyles();
  const theme = useTheme(); 
  ...
}
like image 868
Cipher Avatar asked Dec 24 '19 06:12

Cipher


People also ask

How do you use a theme in makeStyles material UI?

To use theme and props in React Material UI makeStyles , we call it with a function that returns the styles. We can get preset styles of the theme from the theme parameter. And we can set the class property to a function that has props as the parameter and return an object with the style values given the props .

How do I import a theme into Materialui?

import React from 'react'; import mui from 'material-ui'; import injectTapEventPlugin from 'react-tap-event-plugin'; import ThemeManager from 'material-ui/lib/styles/theme-manager'; import Colors from 'material-ui/lib/styles/colors'; import MyTheme from './theme. js'; injectTapEventPlugin(); class App extends React.

What is mui theme?

The theme specifies the color of the components, darkness of the surfaces, level of shadow, appropriate opacity of ink elements, etc. Themes let you apply a consistent tone to your app. It allows you to customize all design aspects of your project in order to meet the specific needs of your business or brand.

How do you use props in makeStyles?

So, this is how to use 'theme' and 'props' in makeStyles. import { Theme } from '@material-ui/core'; import { makeStyles } from '@material-ui/styles'; import { CustomStyleProps } from './example-components. interfaces.


1 Answers

Your custom theme is applied in this line: <ThemeProvider theme={theme}>, and your makeStyles function is called before that.
That means, in this line const classes = useStyles(); you call your makeStyles function, but your theme is still the default theme.
The solution is what you already did, create a CustomizedButton and inside it call the useStyles hook.

like image 87
Ido Avatar answered Oct 04 '22 23:10

Ido