2

The error:

TypeError: Object(...) is not a function
  13 | import { connect } from 'react-redux';
  14 | 
  15 | 
> 16 | const useStyles = makeStyles(theme => ({
  17 |     ...theme
  18 | }));
  19 | 

Code:

const useStyles = makeStyles(theme => ({
    ...theme
}));

Why do I get the above error?
Everything was working but suddenly i got that error, may be after i updated npm npm -g update but i am not sure.

Can someone help out?

Edit: this is how i am using useStyles:

const classes = useStyles();

then i am using the classes to style my elements like this:

<Button className={classes.button} />

classes.button get the button rule from the global theme and use it to style the button

11
  • 1
    Are you able to add more snippets for us to see what's happening in the code? Especially how useStyles is being utilized... Commented Aug 4, 2020 at 2:25
  • 1
    Ok, that's fine. And where are you using the classes variable after declaring it? e.g. classes.input; Are you able to include that too? Commented Aug 4, 2020 at 2:42
  • 1
    Am trying to follow what you want to achieve, does this means that somewhere you have this const theme = createMuiTheme({...}). Where you defined a theme, right? Commented Aug 4, 2020 at 2:58
  • 1
    I was about to ask you this, import { makeStyles } from '@material-ui/core/styles'; (Re-export with a default theme) vs import { makeStyles } from '@material-ui/styles'; (Original module with no default theme). Was that the issue? Because one of those comes with your theme while the other doesn't... Commented Aug 4, 2020 at 3:10
  • 1
    yes, i imported it wrongly like this import { makeStyles} from '@material-ui/core/styles/makeStyles Commented Aug 4, 2020 at 3:12

2 Answers 2

2

To remove the need to systematically supply a theme,
the default Material-UI theme is applied to the re-exported makeStyles modules.

// Re-export with a default theme
import { makeStyles } from '@material-ui/core/styles';

// Original module with no default theme
import { makeStyles } from '@material-ui/styles';

But also be careful of "mistaken" imports.

// for example, this is wrong
import { makeStyles } from '@material-ui/core/styles/makeStyles';

Like what @Code Eagle, here, had fallen into. It can be a pain to debug such errors:)
Here's a good read about this from the Material-UI basics.

Sign up to request clarification or add additional context in comments.

2 Comments

I think this type of nasty errors is really hard to debug, but when i find this error the next time i would go to see if i am imported right :)
The challenge like you had here is that, if it was working the first time, then the last place you suspect is a wrong import statement.:-(
1

When I updated my imports to use the re-exported ref to makeStyles in the core module, I mistakenly failed to delete makeStyles at the end of the import statement when I made the change.

Thank you for the post, I wanted to make the solution a little more explicit for those that update their imports to core.

Bad

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

Good

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.