“无效的挂钩调用。只能在函数组件的主体内部调用挂钩”问题

发布时间:2020-07-07 10:45

我在控制台中收到错误消息

错误! [错误:无效的挂接调用。挂钩只能在内部调用 功能组件的主体。这可能发生在 原因如下:

  1. 您可能使用了不匹配的React和渲染器版本(例如React DOM)
  2. 您可能正在违反挂钩规则
  3. 您可能在同一应用中拥有多个React副本*
   import firebase from '../../database/firebase';
   import { useDispatch } from 'react-redux';
   import * as actions from "../../store/actions";

    export const userProfilePcture =(id)=>{
       const image = firebase.storage().ref(id + '/profilePicture');
       var user = firebase.auth().currentUser;
       image.getDownloadURL().then((url) => {
       user.updateProfile({
          photoURL: url
        }).then(() =>{
          console.log("updete succefully");
          updeteUser();
        }).catch(error =>{
         console.log("error!!",error);
        });
    
      });


      };

       export const updeteUser=()=>{
       const dispatch=useDispatch();
       var user = firebase.auth().currentUser;
       dispatch(actions.updateUser(user));
     }

如何避免这种情况?

回答1

这是您正在使用的挂钩:

const dispatch=useDispatch();

您只能在React功能组件内部使用钩子。现在,您正在尝试在普通的javascript函数中使用此钩子。