【问题标题】:Invalid hook call while using no hooks (material-ui v5)不使用钩子时无效的钩子调用(material-ui v5)
【发布时间】: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


【解决方案1】:

由于您使用的是 material-ui (V5),因此您首先需要更新包名称,然后将导入语句更新为:

import Grid from '@mui/material/Grid';

有关更多信息,请查看更改日志 - Migration from v4 to v5

【讨论】:

  • 刚刚尝试了此更改。没有解决问题,虽然这是新的导入名称。
  • @Frantisek 你能提供一个 CodePen / CodeSandBox 链接,复制相同的场景吗?
  • 我正在尝试,但 CodeSandBox 上的 v5 似乎有点坏...
  • 为此,您必须安装 @mui/material、@emotion/styled 和 @emotion/react 软件包。看看这个 CodeSandBox:codesandbox.io/s/jolly-leftpad-xv15g
  • 现在我不明白...我创建了一个与您发布的项目相同的项目,复制粘贴了所有内容。它编译时没有任何错误/警告。控制台仍然显示该错误。我做错了什么?
猜你喜欢
  • 2021-09-10
  • 2019-12-06
  • 2021-08-04
  • 2019-08-10
  • 2021-06-12
  • 2020-12-02
  • 2021-07-08
  • 2020-02-16
  • 1970-01-01
相关资源
最近更新 更多