简体   繁体   English

无效的钩子调用。 React.js

[英]Invalid hook call. React.js

Import useState from react, paste it to body function, but react tells me Wrong use hooks.从 react 导入 useState,将其粘贴到 body 函数中,但是 react 告诉我错误使用钩子。 Although another hook works like this.虽然另一个钩子是这样工作的。

import {getAuth} from 'firebase/auth';
import {useState} from 'react';

const useAuth = () => {

  const [state, setState] = useState(null); //wrong use

  const auth = getAuth();
  const getUserId = () => {
    return new Promise((resolve, reject) => {
      auth.onAuthStateChanged((user) => {
        if (user) {
          resolve(user.uid);
        } else {
          resolve(null);
        }
      });
    });
  };

  return {
    getUserId,
  };
};

export default useAuth;

Your returned getUserId is not a jsx.您返回的 getUserId 不是 jsx。 If your function is intended to be a react functionnal component then it is supposed to return a jsx.如果您的函数旨在成为反应功能组件,那么它应该返回一个 jsx。 If not then you cannot use a react hook like useState.如果没有,那么你不能使用像 useState 这样的反应钩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 使用 React.js 时 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. while using React.js react.js:无效的钩子调用 - react.js: Invalid hook call 错误:无效的挂钩调用。 但不明白在哪里。 反应 JS - Error: Invalid hook call. But can't understand where. React JS 如何修复 React(TypeScript) 错误“无效的钩子调用。”? - How to fix React(TypeScrtipt) error “Invalid hook call.”? react.js应用程序中的无效钩子调用 - Invalid hook calls in react.js application TypeScript React 无效的挂钩调用错误(“错误:无效的挂钩调用。”) - TypeScript React Invalid Hook Call Error ("Error: Invalid hook call.") react.js - react-alert - 未捕获的不变违规:无效的钩子调用 - react.js - react-alert - Uncaught Invariant Violation: Invalid hook call 无效的挂钩调用 - 在单个 React.js 文件中工作,而不是在 2 个 ReactJS 文件中工作 - Invalid Hook Call - Works in single React.js file, not in 2 ReactJS files 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component 未捕获的错误:无效的挂钩调用。 ReactJs - Uncaught Error: Invalid hook call. ReactJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM