【发布时间】:2022-01-24 18:28:18
【问题描述】:
我有以下简单的应用程序:
import Grid from '@material-ui/core/Grid';
function App() {
return (
<div className="App">
<Grid container spacing={2}>
<Grid item xs={8}>
a
</Grid>
<Grid item xs={4}>
b
</Grid>
<Grid item xs={4}>
c
</Grid>
<Grid item xs={8}>
d
</Grid>
</Grid>
</div>
);
}
export default App;
代码编译时没有错误或警告。然而,在运行时,它会抛出这个:
react.development.js:1476 Uncaught 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.
at resolveDispatcher (react.development.js:1476)
at Object.useContext (react.development.js:1484)
at useTheme (useTheme.js:4)
at useStyles (makeStyles.js:222)
at WithStyles (withStyles.js:55)
at renderWithHooks (react-dom.development.js:14985)
at updateForwardRef (react-dom.development.js:17044)
at beginWork (react-dom.development.js:19098)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
resolveDispatcher @ react.development.js:1476
useContext @ react.development.js:1484
useTheme @ useTheme.js:4
useStyles @ makeStyles.js:222
WithStyles @ withStyles.js:55
renderWithHooks @ react-dom.development.js:14985
updateForwardRef @ react-dom.development.js:17044
beginWork @ react-dom.development.js:19098
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
beginWork$1 @ react-dom.development.js:23964
performUnitOfWork @ react-dom.development.js:22776
workLoopSync @ react-dom.development.js:22707
renderRootSync @ react-dom.development.js:22670
performSyncWorkOnRoot @ react-dom.development.js:22293
scheduleUpdateOnFiber @ react-dom.development.js:21881
updateContainer @ react-dom.development.js:25482
(anonymous) @ react-dom.development.js:26021
unbatchedUpdates @ react-dom.development.js:22431
legacyRenderSubtreeIntoContainer @ react-dom.development.js:26020
render @ react-dom.development.js:26103
./src/index.js @ index.js:7
是我做错了什么还是最新的material-ui 中存在错误?我已经在谷歌上搜索了几个小时,但没有找到解决这个问题的方法。
【问题讨论】:
-
页面上有两个版本的 React 吗?
-
@JaredSmith 不,这是一个使用 create-react-app 创建的干净的新项目。
标签: javascript reactjs material-ui