【发布时间】:2021-12-07 09:32:59
【问题描述】:
我想做 Create-react-app 并使用 Material UI,但我收到了 Hooks 错误。
我是否错过了其他东西?
这是一个错误声明。
错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:
- 您可能有不匹配的 React 版本和渲染器(例如 React DOM)
- 您可能违反了 Hooks 规则
- 您可能在同一个应用程序中拥有多个 React 副本 有关如何调试和解决此问题的提示,请参阅 https://reactjs.org/link/invalid-hook-call。
我做了以下。
$ 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;
【问题讨论】:
标签: reactjs typescript material-ui