3

I'm integrating Material-UI (v4.4.3) into a React (v16.9.2) TypeScript (v3.6.3) website. Using the sample AppBar component example https://material-ui.com/components/app-bar/ and the TypeScript Guide https://material-ui.com/guides/typescript/#typescript I have the following functional component.

However, I'm getting a TS error for useStyles() on this line

const classes = useStyles();

(TS): Expected one argument, but got 0.

import * as React from 'react';

import { createStyles, Theme, makeStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';

const useStyles = makeStyles(({ spacing }: Theme) =>
  createStyles({
    root: {
      flexGrow: 1,
    },
    menuButton: {
      marginRight: spacing(2),
    },
    title: {
      flexGrow: 1,
    },
  }),
);

export default function ButtonAppBar() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <AppBar position="static">
        <Toolbar>
          <IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="menu">
            <MenuIcon />
          </IconButton>
          <Typography variant="h6" className={classes.title}>
            MSC
          </Typography>
          <Button color="inherit">Login</Button>
          )}
        </Toolbar>
      </AppBar>
    </div>
  );
}

I want to use the default theme. Am I missing something?

1
  • What I'm not entirely clear about from the docs and samples is what, if anything, you have to do to make the default theme available to all components. Commented Sep 24, 2019 at 21:36

2 Answers 2

2

Try to pass an empty object:

const classes = useStyles({});
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this results in a "TypeError: spacing is not a function" error.
I copied and pasted your code and passed {} to the useStyles function as I showed in the answer. No error occurs.
1

try using components from :

import AppBar from "@material-ui/core/AppBar"
import XXX from "@material-ui/core/xxx"

XXX = other components

notes my dependencies are:

  • dependencies : "@material-ui/core": "^4.9.14"
  • devDependencies : "@types/material-ui": "^0.21.7"

don't forgot to import @types/material-ui devDependencies in your project.

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.