[英]React Native. Error: Invalid hook call. Hooks can only be called inside of the body of a function component
My App was working fine and suddenly i got this error.我的应用程序运行良好,突然我收到了这个错误。
Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.
钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:
这可能由于以下原因之一而发生:
You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
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.您可能在同一个应用程序中拥有多个 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;
All looks fine.一切看起来都很好。 except maybe actually importing React
除了可能实际导入 React
import React, { useContext } from "react";
I know you don't need this for React from React 17, but there's no official statement from react native saying they use the new JSX compiler that doesn't require the import statement我知道 React 17 中的 React 不需要这个,但是 react native 没有官方声明说他们使用不需要 import 语句的新 JSX 编译器
also check the AuthContext file you imported还要检查您导入的 AuthContext 文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.