Material-ui: The create-react-app example does not work: [Theme] spacing function is not recognized.

Created on 9 Feb 2019  路  19Comments  路  Source: mui-org/material-ui

  • [x] This is not a v0.x issue.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 馃

The example found at material-ui/examples/create-react-app/ should work flawlessly right outside of the box.

Current Behavior 馃槸

The theme's spacing function is not recognized. The error is caught in the srcpagesindex.js file, line 16:
paddingTop: theme.spacing(20)

The error message is: TypeError: theme.spacing is not a function

Steps to Reproduce 馃暪


Links:
Repo forked from the original (no changes made yet).
Sandboxed repo.

  1. Gather the create-react-app example project files to start a new React application.
  2. Install it and run. For example:
npm install
npm run start
  1. You should be able to catch the error by now.

Context 馃敠

Just trying to run the example application out of the box.

Your Environment 馃寧


According to the package-lock.json file:

| Tech | Version |
|--------------|---------|
| Material-UI | v3.9.2 |
| React | v16.8.1 |
| Chrome | v72.0.3626.96 |
| TypeScript | None |
| create-react-app | v3.2.2 |

Root cause hypothesis

Perhaps this issue could be related to #14280 ?

Most helpful comment

Thank you, Matt. I will try using a previous commit version and we will see how it goes. Just keep in mind the confusion this might cause to newcomers trying to learn Material-UI by running the examples provided in the official documentation.

All 19 comments

Perhaps this issue could be related to #14280 ?

Yes, or more specifically, this PR: #14099

theme.spacing has been updated, however aside from the fact that it hasn't been published in a release yet, the example use latest, not next, so I don't believe thy should have been changed. I'm guessing it was a global search & replace SNAFU.

It should just be a question of checking out examples directory from a prior commit, and committing.

Thank you, Matt. I will try using a previous commit version and we will see how it goes. Just keep in mind the confusion this might cause to newcomers trying to learn Material-UI by running the examples provided in the official documentation.

Oops, my bad. I think that we should duplicate all our demos targeting latest and next.

Thank you, Olivier.

I will keep an eye on this until it's solved.

I am that newcomer lesair talked about. It works on its own, but when you try to add Dashboard layout to it it throw an error: "theme.spacing is not a function." Because Dashboard.js uses the new thing, but the example does not support it. How can one update the example? I have installed all the latest packages.

@farid64 You should follow the links in the documentation.

(You need to use the master branch: https://github.com/mui-org/material-ui/blob/master/docs/src/pages/getting-started/page-layout-examples/dashboard/Dashboard.js)

@farid64 Make sure you are using the master branch of the examples repo.

Hi. Thanks for responses. I think I did use the master branch. I followed the link from the documentation.
I'm confused. Is the spacing function new feature or using 'theme.spacing.unit * 2' a new feature? Because if you follow the links that you guys sent me, it actually uses the latter method, '..unit * 2'.

Can you clarify?
By the way, I am using the latter method, ' ..spacing.unit *2', and it is working right now.

theme.spacing.unit is deprecated.

The new spacing feature is documented in here https://next.material-ui.com/customization/themes/#spacing.

master branch didnt work for me either. just ended up commenting out the spacing lines.

@amcquade agreed. just checked out master. same theme.spacing error is thrown when i copy Dashboard.js into a new project and try to use the component as-is

Can somebody point a line where we do it wrong (using a GitHub URL)?

sorry, turns out i was on the "next" branch, which _does_ have the error.

the master branch now has the theme.spacing.unit instead of the theme.spacing() call, which differs from the original

from _master_ branch, Dashboard.js:83
content: {
flexGrow: 1,
padding: theme.spacing.unit * 3,
height: '100vh',
overflow: 'auto',
},

@amcquade This is the next branch, how did you arrive here?

@oliviertassinari neither master nor next branch worked for me.

@oliviertassinari I just experienced this problem, but the link you provided for the master branch worked for me. Thanks!

I replaced the padding: theme.spacing(1) statements in the
https://github.com/mui-org/material-ui/blob/master/docs/src/pages/premium-themes/paperbase/
example and it worked for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

revskill10 picture revskill10  路  3Comments

TimoRuetten picture TimoRuetten  路  3Comments

FranBran picture FranBran  路  3Comments

chris-hinds picture chris-hinds  路  3Comments

rbozan picture rbozan  路  3Comments