![](/img/trans.png)
[英]React Native: Error: Invalid hook call. Hooks can only be called inside of the body of a function component
[英]React Native. Error: Invalid hook call. Hooks can only be called inside of the body of a function component
我的应用程序运行良好,突然我收到了这个错误。
错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 这可能由于以下原因之一而发生:
你可能有不匹配的 React 版本和渲染器(例如 React DOM)
您可能违反了 Hooks 规则
您可能在同一个应用程序中拥有多个 React 副本,请参阅https://reactjs.org/link/invalid-hook-call以获取有关如何调试和修复此问题的提示。
import { useContext } from "react"; import jwtDecode from "jwt-decode"; import AuthContext from "./context"; import authStorage from "./storage"; const useAuth = () => { const { user, setUser } = useContext(AuthContext); const logIn = (authToken) => { const user = jwtDecode(authToken); setUser(user); authStorage.storeToken(authToken); }; const logOut = () => { setUser(null); authStorage.removeToken(); }; return { user, logIn, logOut }; }; export default useAuth;
一切看起来都很好。 除了可能实际导入 React
import React, { useContext } from "react";
我知道 React 17 中的 React 不需要这个,但是 react native 没有官方声明说他们使用不需要 import 语句的新 JSX 编译器
还要检查您导入的 AuthContext 文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.