Material-ui: Cannot read property 'direction' of null

Created on 28 Dec 2018  路  5Comments  路  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 馃


I use this code

const useStyles = makeStyles(theme => ({
  main: {
    width: 'auto',
    display: 'block', // Fix IE 11 issue.
    marginLeft: theme.spacing.unit * 3,
    marginRight: theme.spacing.unit * 3,
    [theme.breakpoints.up(400 + theme.spacing.unit * 3 * 2)]: {
      width: 400,
      marginLeft: 'auto',
      marginRight: 'auto',
    },
  },
  paper: {
    marginTop: theme.spacing.unit * 8,
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    padding: `${theme.spacing.unit * 2}px\
    ${theme.spacing.unit * 3}px\
    ${theme.spacing.unit * 3}px`,
  },
  avatar: {
    margin: theme.spacing.unit,
    backgroundColor: theme.palette.secondary.main,
  },
  form: {
    width: '100%', // Fix IE 11 issue.
    marginTop: theme.spacing.unit,
  },
  submit: {
    marginTop: theme.spacing.unit * 3,
  },
}));

And want it to use in my functional component

like this

const classes = useStyles();

Current Behavior 馃槸


This error pops up.

Cannot read property 'direction' of null

127 | var options = (0, _extends2.default)({}, stylesCreator.options, stylesOptions, {
> 128 |   flip: typeof stylesOptions.flip === 'boolean' ? stylesOptions.flip : theme.direction === 'rtl'
      | ^  129 | });
  130 | var sheetsRegistry = stylesOptions.sheetsRegistry;

Steps to Reproduce 馃暪


Link: https://github.com/hayk94/react2019app

  1. clone it
  2. npm i
  3. npm start

Context 馃敠

I want to use useStyles hook

Your Environment 馃寧

| Tech | Version |
|--------------|---------|
| Material-UI | next |
| React | next |
| Browser | chrome |

discussion

All 5 comments

Are you properly injecting ThemeProvider higher in the component tree?

In the stable version, you'd nest JssProvider and MuiThemeProvider. Now you need to replace those with:

<StylesProvider
    generateClassName={/* */}
    sheetsRegistry={/* */}
    sheetsManager={/* */}>
    <ThemeProvider theme={/* */}>
       {/* your stuff */}
    </ThemeProvider>
</StylesProvider>

@dandrei I got the theme provider.

Here is my App.js

import React from 'react';
import { MuiThemeProvider } from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import {
  Switch,
} from 'react-router-dom';
import theme from './theme';
import Auth from './Auth';

const App = () => (
  <MuiThemeProvider theme={theme}>
    <CssBaseline />
    <Switch>
      <Auth />
    </Switch>
  </MuiThemeProvider>
);

export default App;

and Auth.js

// @flow
import * as React from 'react';
import {
  Route,
} from 'react-router-dom';
import {
  Switch,
} from 'react-router-dom';
import Login from './Login';
import Reset from './Reset';
import SignPage from './SignPage';
import Register from './Register';

const Auth = () => {
  return (
    <Switch>
      <Route exact path="/login" component={Login} />,
      <Route exact path="/reset/:code" component={Reset} />,
      <Route exact path="/signpage/:code" component={SignPage} />,
      <Route exact path="/register/:code" component={Register} />,
    </Switch>
  );
};

export default Auth;

And Login.js

// @flow
import * as React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import FormControl from '@material-ui/core/FormControl';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Input from '@material-ui/core/Input';
import InputLabel from '@material-ui/core/InputLabel';
import LockIcon from '@material-ui/icons/LockOutlined';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/styles';

const useStyles = makeStyles(theme => ({
  main: {
    width: 'auto',
    display: 'block', // Fix IE 11 issue.
    marginLeft: theme.spacing.unit * 3,
    marginRight: theme.spacing.unit * 3,
    [theme.breakpoints.up(400 + theme.spacing.unit * 3 * 2)]: {
      width: 400,
      marginLeft: 'auto',
      marginRight: 'auto',
    },
  },
  paper: {
    marginTop: theme.spacing.unit * 8,
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    padding: `${theme.spacing.unit * 2}px\
    ${theme.spacing.unit * 3}px\
    ${theme.spacing.unit * 3}px`,
  },
  avatar: {
    margin: theme.spacing.unit,
    backgroundColor: theme.palette.secondary.main,
  },
  form: {
    width: '100%', // Fix IE 11 issue.
    marginTop: theme.spacing.unit,
  },
  submit: {
    marginTop: theme.spacing.unit * 3,
  },
}));

const Login = (props) => {
  const classes = useStyles();

  return (
    <main className={classes.main}>
      <Paper className={classes.paper}>
        <Avatar className={classes.avatar}>
          <LockIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign in
        </Typography>
        <form className={classes.form}>
          <FormControl margin="normal" required fullWidth>
            <InputLabel htmlFor="email">Email Address</InputLabel>
            <Input id="email" name="email" autoComplete="email" autoFocus />
          </FormControl>
          <FormControl margin="normal" required fullWidth>
            <InputLabel htmlFor="password">Password</InputLabel>
            <Input
              name="password"
              type="password"
              id="password"
              autoComplete="current-password"
            />
          </FormControl>
          <FormControlLabel
            control={<Checkbox value="remember" color="primary" />}
            label="Remember me"
          />
          <Button
            type="submit"
            fullWidth
            variant="contained"
            color="primary"
            className={classes.submit}
          >
            Sign in
          </Button>
        </form>
      </Paper>
    </main>
  );
};

export default Login;

According to the docs there is no need for styles provider to use theme.

https://material-ui.com/css-in-js/advanced/

@hayk94 if useStyles references a theme, a theme needs to be provided.

In my code I had to use the following, to work with the new stuff:

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

instead of the old:

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

We have isolated the styling solution in a @material-ui/styles package. It's a standalone solution. It has no knowledge of the material-ui core components. It also means that there is no default theme, especially not the Material-UI default theme.

In the future, I'm wondering if we should have @material-ui/core/styles exposes the same modules as @material-ui/styles but with a default theme. cc @mui-org/core-contributors

@oliviertassinari that makes sense since it would be the Material-UI implementation of the styles package.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tleunen picture tleunen  路  59Comments

tdkn picture tdkn  路  57Comments

chadobado picture chadobado  路  119Comments

mnajdova picture mnajdova  路  105Comments

darkowic picture darkowic  路  62Comments