1

I really don't understand what am I doing wrong.

I am using the makeStyles hook as described inside the (javascript) documentation, and I am getting this Invalid Hook Call. I am using TypeScript 4.1.2, React 17.0.1 and React Types 17.0.0.

Here is the component code:

import React from 'react';
import { Theme, makeStyles } from '@material-ui/core/styles';

interface Props
{
    isVisible: boolean;
}

const useStyles = makeStyles( ( theme: Theme ) => ( {
    root: {
        background: 'red',
        height: 100,
    },
} ) );

const Nav: React.FC<Props> = ( props: Props ) =>
{
    const { isVisible } = props;

    const classes = useStyles( {} );

    return (
       <div className={classes.root}>

       </div>
    );
};

export default Nav;

Error message:

1 Answers1

1

The answer was simpler than I would have thought. It seems that with "react-scripts":"4.0.3", the code works. I don't remember what version of cra I was using previously(I updated it before I checked), but with the latest version looks like it is working.

Here is what I did:

  1. I created a new branch in my git.
  2. I completely deleted the app
  3. I typed in the terminal npm i -g create-react-app
  4. I created a new react app: create-react-app <your file> --template typescript
  5. Finally I added the dependencies and libraries: npm i @material-ui/core @material-ui/icons @material-ui/lab <other-libraries-here> ... @types/material-ui <other-types-here> ...
  6. I changed, as I did before, the tsconfig.json file as the material-ui documentation suggests (I left the strictNullChecks to false as a personal preference)
  7. I ran the start script using npm/yarn start.

I hope this answer helps somebody that encountered this problem! Happy hacking!