React withStyles throws error when using 'direction'

1.4k Views Asked by At

I have a simple React website and am using the withStyles feature to pass styles to a material-ui Grid element. When trying to use direction: "column" in the style, I get the error message included below.

This is in a .jsx file. I have found a fix by specifying type, but that only works for TypeScript.

Here is the App.jsx file:


const styles = theme => ({
    root: {
        direction: "column",
        justify: "center",
        alignItems: "center",
    },
});

function App(props) {

    return (
        <div>
            <Grid container className={props.classes.root} >
                <Typography variant="h2">
                    Test Website
                </Typography>
                <TextField
                    id="input-user"
                    label="User"
                    value={1}
                    margin="normal"
                    variant="outlined"
                />

            </Grid>
        </div>
    )

}

export default withStyles(styles)(App);

The error message I am getting is this:

Argument of type '(theme: any) => { root: { direction: string; justify: string; alignItems: string; }; }' is not assignable to parameter of type 'Styles<Theme, {}, "root">'.
  Type '(theme: any) => { root: { direction: string; justify: string; alignItems: string; }; }' is not assignable to type 'StyleRulesCallback<Theme, {}, "root">'.
    Type '{ root: { direction: string; justify: string; alignItems: string; }; }' is not assignable to type 'Record<"root", CreateCSSProperties<{}> | ((props: {}) => CreateCSSProperties<{}>)>'.
      Types of property 'root' are incompatible.
        Type '{ direction: string; justify: string; alignItems: string; }' is not assignable to type 'CreateCSSProperties<{}> | ((props: {}) => CreateCSSProperties<{}>)'.
          Type '{ direction: string; justify: string; alignItems: string; }' is not assignable to type 'CreateCSSProperties<{}>'.
            Types of property 'direction' are incompatible.
              Type 'string' is not assignable to type '"-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "ltr" | "rtl" | ((props: {}) => DirectionProperty)'.

Thank you for any help.

2

There are 2 best solutions below

4
Kareem Dabbeet On BEST ANSWER

this is because there is no type column in direction property. it can take a string of type:

  • ltr
  • rtl
  • initial
  • inherit
0
Fred On

You could also try the following line export default withStyles(styles as {})(App); which saved me.