[英]I get the Error message: “Invalid hook call” when I try to run my React app. Can't understand why
I get the error message: Invalid hook call. Hooks can only be called inside of the body of a function component
我收到错误消息:
Invalid hook call. Hooks can only be called inside of the body of a function component
Invalid hook call. Hooks can only be called inside of the body of a function component
when I try to run my react app.当我尝试运行我的 react 应用程序时
Invalid hook call. Hooks can only be called inside of the body of a function component
。
import React from "react"; import BarChart from "../components/BarChart"; import LineChart from "../components/LineChart"; import Navbar from "../components/Navbar"; import { makeStyles } from "@material-ui/core/styles"; const useStyles = makeStyles({ row: { display: "flex", flexDirection: "row" } }); const Index = () => { const classes = useStyles(); return ( <div> <Navbar /> <div className={classes.row}> <BarChart /> <LineChart /> </div> </div> ); }; export default Index;
It fails in line 16 ( const classes = useStyles();
).它在第 16 行失败(
const classes = useStyles();
)。
The Navbar, BarChart and LinChart components is just components that I have created and it doesn't seem like the code breaks in these components. Navbar、BarChart 和 LinChart 组件只是我创建的组件,这些组件中的代码似乎没有中断。 I'm able to run my code without any error messages if iI remove line 16.
如果我删除第 16 行,我可以运行我的代码而不会出现任何错误消息。
Any ideas what might help?有什么想法可能有帮助吗?
I tried makeStyles locally and it worked perfectly fine.我在本地尝试了 makeStyles,它工作得非常好。 Kindly check your dependencies and update them to latest then try again.
请检查您的依赖项并将它们更新到最新版本,然后重试。 I'm currently having:
"react": "^16.8.2", "react-dom": "^16.8.2", "@material-ui/core": "^4.9.12"
我目前有:
"react": "^16.8.2", "react-dom": "^16.8.2", "@material-ui/core": "^4.9.12"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.