简体   繁体   English

REACT - 未处理的拒绝(错误):无效的挂钩调用

[英]REACT - Unhandled Rejection (Error): Invalid hook call

So i am trying to use the useHistory of react-router-dom package using the below code.所以我正在尝试使用下面的代码来使用 react-router-dom package 的 useHistory 。

function AdminLogin() {

    const LoginAct = async () => {    
        const requestOptions = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ password: hash })
        };
        await fetch('/post/admin/login', requestOptions).then(HandleResponse);
    }
    
    const HandleResponse = async (response) => {
        return response.text()
            .then(text => {
            if (response.ok) {
                var data = text && JSON.parse(text);
                data = data[0];
                if (data != null) {
                    LoginRoute();
                }
            }
        })
    }

    function LoginRoute() {
        const history = useHistory(); 
        history.push('/student/app');
    }

    return (
            // View here including button that calls LoginAct when clicked
        );

}

export default AdminLogin;

However I am facing this error from const history = useHistory();但是我正面临来自const history = useHistory();的错误。 :

在此处输入图像描述

I have tried to debug this with instructions in the URL shown in the error message.我尝试使用错误消息中显示的 URL 中的说明进行调试。 No luck: My react and React DOM versions:不走运:我的 react 和 React DOM 版本:

  "peerDependencies": {
    "react": "^17.0.2"
  },
  "dependencies": {
    "react-dom": "^17.0.2",
  },

I have placed the react package to peerDependecies as instructed in some of the answers here .我已按照此处的一些答案中的说明将反应 package 放置到 peerDependecies 。 I have also tested other solutions found from the github issue above for ex.我还测试了从上面的 github 问题中找到的其他解决方案,例如。 clearing my npm cache and updated all my packages清除我的 npm 缓存并更新我所有的包

I have no idea what would be causing this problem other than me breaking Rules of Hooks, in which case I don't know how am I breaking them.除了我违反 Hooks 规则之外,我不知道是什么导致了这个问题,在这种情况下,我不知道我是如何破坏它们的。 (I also have eslint installed to enforce Rules of Hooks but it is possible that I have set it up wrong) (我也安装了 eslint 来执行 Hooks 规则,但我可能设置错了)

The hook needs to be called at the top level of the component.钩子需要在组件的顶层调用。 There's also no reason to abstract the single line into an additional callback, just do the PUSH in the asynchronous handler.也没有理由将单行抽象为额外的回调,只需在异步处理程序中执行 PUSH。

function AdminLogin() {
    const history = useHistory(); // <-- here

    const LoginAct = async () => {    
        const requestOptions = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ password: hash })
        };
        await fetch('/post/admin/login', requestOptions).then(HandleResponse);
    }
    
    const HandleResponse = async (response) => {
        return response.text()
            .then(text => {
            if (response.ok) {
                var data = text && JSON.parse(text);
                data = data[0];
                if (data != null) {
                    history.push('/student/app'); // <-- called here
                }
            }
        })
    }

    return (
            // View here including button that calls LoginAct when clicked
    );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM