Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

theme.spacing is not a function with @material-ui/[styles/core] 4.0.1 #15976

Closed
2 tasks done
keul opened this issue May 31, 2019 · 23 comments
Closed
2 tasks done

theme.spacing is not a function with @material-ui/[styles/core] 4.0.1 #15976

keul opened this issue May 31, 2019 · 23 comments
Labels
support: question Community support but can be turned into an improvement

Comments

@keul
Copy link
Contributor

keul commented May 31, 2019

Trying to fix another issue today I tried to reset my whole Material-UI 4.0.0 workspace, migrating both core and style to latest 4.0.1

As soon as I try to use a ThemeProvider I get TypeError theme.spacing is not a function.

Expected Behavior 🤔

Just want to see the theming working. I'm confused by the fact that theme.spacing seems used by both master and next branches, so this seems the way to go.

Current Behavior 😯

See error above

Steps to Reproduce 🕹

Link: https://codesandbox.io/s/materialui-style-issue-rk8n0

Context 🔦

I started a whole new application and I'm trying to clue together all of the style feature of Material-UI. That said, before the migration to 4.0.1 of style module I had not such issues, so I'm unsure if 4.x releases are someway unstable or what.

Your Environment 🌎

Tech Version
Material-UI v4.0.1
React 16.0.6
Browser FF/Chrome latest
TypeScript -
@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label May 31, 2019
@oliviertassinari
Copy link
Member

@keul Be aware of the difference between core and styles: https://material-ui.com/customization/default-theme/#material-ui-core-styles-vs-material-ui-styles.

@keul
Copy link
Contributor Author

keul commented May 31, 2019

Thanks @oliviertassinari.

Material-UI styles are powered by the @material-ui/styles npm package. It's a styling solution for React. This solution is isolated, it has has no knowledge of the default Material-UI theme. To remove the need for injecting a theme in the React's context systematically, we are wrapping the style modules (makeStyles, withStyles and styled) with the default Material-UI theme

To be honest I don't get the point here, nor how this is related to the issue. This means than I don't need the ThemeProvider because already shipped when importing from core/styles?

Just to give additional context: my code was working using core 4.0.0 and styles 3.0.0-alpha.10
I started having issue this morning, after migrating to 4.0.1

I can quickly go back to old package-lock version, but I'd like to get the big picture

@oliviertassinari
Copy link
Member

@keul Alright, maybe the following will be clearer. You are injecting a new theme that is not compatible with the core components. You should use createMuiTheme.

import { createMuiTheme } from "@material-ui/core";

const theme = createMuiTheme({
  spacing: 4,
  palette: {
    primary: {
      main: "#007bff",
    },
  }
});

@keul
Copy link
Contributor Author

keul commented May 31, 2019

@oliviertassinari well... thanks. This fixed the issue.

I'm pretty sure I copied this raw code from the documentation, it's the same way-to-go I find at https://material-ui.com/styles/advanced/. As a new user (last time I used MUI it was version 0.x) this is not really clear but I probably have to give it some time.

Sorry for the noise!

@oliviertassinari
Copy link
Member

oliviertassinari commented May 31, 2019

Anything in https://material-ui.com/styles/x is Material Design unrelated. It's a styling solution for React.

@ByronHsu
Copy link

ByronHsu commented Jun 4, 2019

Sorry, I still can't understand.
Can we send theme as a argument to styles, and wrap it by withstyles? Like the following.

const styles = theme => {({
    root: {
      // JSS uses px as the default units for this CSS property.
      padding: theme.spacing(2), // = 8 * 2
    },
})};

@oliviertassinari
Copy link
Member

Can we send theme as a argument to styles, and wrap it by withstyles? Like the following.

@ByronHsu Yes, if you import from @material-ui/core/styles or if you inject a theme created with createMuiTheme().

@ByronHsu
Copy link

ByronHsu commented Jun 6, 2019

But it return theme.spacing is not a function.

@oliviertassinari
Copy link
Member

@ByronHsu Do you have a reproduction?

@ByronHsu
Copy link

ByronHsu commented Jun 7, 2019

Sorry I found the problem.
I change import { withStyles } from '@material-ui/styles'; to import { withStyles } from '@material-ui/core/styles'; and everything works fine.
Thanks!

@TidyIQ
Copy link
Contributor

TidyIQ commented Aug 8, 2019

I'm having this issue as well and I'm using

import { makeStyles, Theme } from "@material-ui/core/styles";

const useStyles = makeStyles((theme: Theme) => ({
  icon: {
    marginRight: theme.spacing(1)
  }
}));

This component is wrapped in a <ThemeProvider />

@TidyIQ
Copy link
Contributor

TidyIQ commented Aug 8, 2019

Same issue occurs if I instead use:

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

const useStyles = makeStyles((theme: Theme) => ({
  icon: {
    marginRight: theme.spacing(1)
  }
}));

I get:

TypeError: theme.spacing is not a function

All MUI packages are up to date, master branch.

@TidyIQ
Copy link
Contributor

TidyIQ commented Aug 8, 2019

Nevermind, rookie mistake. I forgot to wrap my new theme in createMuiTheme - I was returning a basic object instead.

@medmin
Copy link

medmin commented Aug 10, 2019

same here

@jonkelling
Copy link
Contributor

jonkelling commented Aug 13, 2019

I see this issue if I do everything as expected, except code for an outer theme when there isn't one. For example:

breaks:

<MuiThemeProvider theme={outer => ({...outer, ...theme})}>...</MuiThemeProvider>

works:

<MuiThemeProvider>
  <MuiThemeProvider theme={outer => ({...outer, ...theme})}>...</MuiThemeProvider>
</MuiThemeProvider>

I see there is a warning for this case. It seems like it should still work, though, or at least leave that logic to the provided function.

@x5engine
Copy link

x5engine commented Sep 2, 2019

go from
marginRight: theme.spacing(1)

change it to
marginRight: theme.spacing.unit

@tristanMatthias
Copy link

This is also an issue for me. It seems to be a type error, because the I'm logging the value itself and it's working, but next.js throws an error saying it doesn't exist. Breakpoints too.

@ambekar92
Copy link

Sorry I found the problem.
I change import { withStyles } from '@material-ui/styles'; to import { withStyles } from '@material-ui/core/styles'; and everything works fine.
Thanks!

Thank you @ByronHsu

its working

@Vibek-D
Copy link

Vibek-D commented Jul 13, 2021

Same issue occurs if I instead use:

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

const useStyles = makeStyles((theme: Theme) => ({
  icon: {
    marginRight: theme.spacing(1)
  }
}));

I get:

TypeError: theme.spacing is not a function

All MUI packages are up to date, master branch.

How did you solve it, m having same issue

@HieDave
Copy link

HieDave commented Aug 20, 2021

Same issue occurs if I instead use:

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

const useStyles = makeStyles((theme: Theme) => ({
  icon: {
    marginRight: theme.spacing(1)
  }
}));

I get:

TypeError: theme.spacing is not a function

All MUI packages are up to date, master branch.

How did you solve it, m having same issue

@HieDave
Copy link

HieDave commented Aug 20, 2021

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

@mnajdova
Copy link
Member

@Dave7776 if you are using v4 both should work. If you are using v5 you need to import from '@material-ui/styles' and have a ThemeProvider at the top of your app. Take a look on the migration guide for more details: https://next.material-ui.com/guides/migration-v4/#main-content

@Jkz2025
Copy link

Jkz2025 commented Jan 14, 2023

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

Hey bro llevaba tiempo buscando esta solucion muchas gracias de verdad respuesta clara, sincera y sencilla.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests