简体   繁体   English

React Redux 给出错误:无效的挂钩调用。 钩子只能在 function 组件的内部调用

[英]React Redux giving error : Invalid hook call. Hooks can only be called inside of the body of a function component

I am new to react and redux, I am trying to call dispatch of redux but I am getting this error.我是 redux 的新手,我正在尝试调用 redux 的调度,但出现此错误。

Invalid hook call.挂钩调用无效。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的内部调用。

Here is the scenario, I have separated axios calls to separate file and call it services and importing the same to components wherever it is required.这是场景,我将 axios 调用分离到单独的文件并将其称为服务,并将其导入到需要的组件中。 If the service call returns 401, unauthorised, I want to navigate to Login page as well as clear the auth token that i stored in redux store.如果服务调用返回 401,未经授权,我想导航到登录页面并清除我存储在 redux 商店中的身份验证令牌。

Below is my code:下面是我的代码:

import axios from "axios";
import React from "react";
import { endPoints } from "./endPoints";
import storeSlice from "../Store/storeSlice";
import { updateAuth } from "../Store/storeSlice";
import { useNavigate } from 'react-router-dom';
import { useDispatch } from "react-redux";

export const getIncidentData = async (query, authToken) => {
    return await getData(endPoints.getIncidents + query, authToken)
}

function Logout() {
    const navigate = useNavigate();
    const dispatch = useDispatch();
    const logout = function (e) {
        dispatch(updateAuth({
            authToken: ""
        }));
        navigate('/Login');
        e.stopPropagation();
    }
    return {logout};
}
const getData = async (url, authToken) => {
    const response = await axios({
        method: "get",
        url: endPoints.baseUrl + url,
        headers: {
            'x-access-token': authToken,
            "Access-Control-Allow-Origin": "*",
            'Content-Type': 'application/json',
            'X-AUTH-TOKEN': authToken
        },
        data: {},
    }).then((response) => {
        if (response.success)
            return response;
        if (response.errorCode == 401)
            Logout().logout();
    }).catch(error => {
        if (error.response.data.errorCode == 401)
            Logout().logout();
        const message = error ? error.code != "ERR_NETWORK" ? error.response.statusText : "Netwrok Error" : "Netwrok Error";
        return {
            data: {
                success: false,
                message: message,
                status: error.response.status
            }
        }
    });
}

useDispatch is only can be used on functional component. useDispatch 只能用在功能组件上。 So, you should change the code like this.因此,您应该像这样更改代码。

function Logout() {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const logout = function () {
      dispatch(updateAuth({
          authToken: ""
      }));
      navigate('/Login');
  }
  return <Button onClick={() => logout()}>Logout</Button>;
}

its because the functional component must return the view, or html tag component.这是因为功能组件必须返回视图,或 html 标签组件。

But, if you want to use the Logout as a functions, Usually I use this method:但是,如果你想将注销用作函数,通常我使用这种方法:

logout.js注销.js

import { updateAuth } from '../Store/storeSlice';
import { useNavigate } from 'react-router-dom';

export const logout = () => (dispatch) => {
    dispatch(updateAuth(
        authToken: ''
    ));
    navigate('/login');
};

and for the view or component page:对于视图或组件页面:

ExampleView.jsx ExampleView.jsx

import React from 'react';
import { logout } from '../../logout.js';
import { useDispatch } from "react-redux";

function ExampleView() {
    const dispatch = useDispatch();
    return (
        <div>
            <p>Is My Text</p>
            <Button onClick={() => dispatch(logout())}>Logout</Button>
        </div>
    );
};

暂无
暂无

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

相关问题 反应 Redux - 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Redux - Error: Invalid hook call. Hooks can only be called inside of the body of a function component 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component React Native 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React Native Error: Invalid hook call. Hooks can only be called inside of the body of a function component 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 通过路由反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react 错误:无效的挂钩调用。 钩子只能在 react-native 中的 function 组件的主体内部调用 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component in react-native 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React 反应,得到错误:无效的钩子调用。 Hooks 只能在函数组件的主体内部调用 - React, getting Error: Invalid hook call. Hooks can only be called inside of the body of a function component React - 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component React Native:错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native: Error: Invalid hook call. Hooks can only be called inside of the body of a function component React Hooks + Mobx =&gt; 无效的钩子调用。 Hooks 只能在函数组件内部调用 - React Hooks + Mobx => Invalid hook call. Hooks can only be called inside of the body of a function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM