Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create-react-app MaterialUI Error: Invalid hook call

I want to do Create-react-app and use Material UI, but I get a Hooks Error.

Am I missing something else?

This is an error statement.

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

I did the following.

$ npx create-react-app my-app --template typescript

$ npm install @mui/material

$ npm install @mui/icons-material

package.json

{
  "name": "muitest",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@mui/icons-material": "^5.2.1",
    "@mui/material": "^5.2.3",
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "@types/jest": "^26.0.24",
    "@types/node": "^12.20.37",
    "@types/react": "^17.0.37",
    "@types/react-dom": "^17.0.11",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "typescript": "^4.5.2",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

App.tsx

import React from 'react';
import './App.css';
import GitHubIcon from '@mui/icons-material/GitHub';

function App() {
  return (
    <div className="App">
        <GitHubIcon />
    </div>
  );
}

export default App;
like image 425
natsuyu Avatar asked Feb 28 '26 13:02

natsuyu


1 Answers

https://create-react-app.dev/docs/adding-typescript/ https://github.com/mui-org/material-ui

please go through this both link, u need to add few packages then it will work.

npx create-react-app my-app --template typescript
npm install @mui/material @emotion/react @emotion/styled @types/material-ui

for typescript we need to install few package @types packages.

as shown its working for me

like image 179
Soniya Rana Avatar answered Mar 02 '26 05:03

Soniya Rana