React Hook "useDispatch" 在函数"requestWithAction"中调用,该函数既不是 React 函数组件也不是自定义 React Hook 函数



当我使用react redux("react-redux": "^7.2.2"(在从服务器获取数据后显示操作时,如下所示:

import axios from 'axios';
import store from "../store";
import { getArticle } from "../action/ArticleAction";
import { useDispatch } from 'react-redux';

export function requestWithAction(config, action) {
const dispatch = useDispatch();
return axios(config).then(
response => {
const data = response.data.result;
dispatch({ type: 'SUBMIT_DECISION' , payload: 1});
//dispatch(action(data));
}
).catch(
error => {
console.error(error);
}
);
}

显示如下错误:

Failed to compile
src/common/XHRClient.js
Line 20:20:  React Hook "useDispatch" is called in function "requestWithAction" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter  react-hooks/rules-of-hooks
Search for the keywords to learn more about each error.
This error occurred during the build time and cannot be dismissed.

是否可以在http响应函数中进行dispath?如果我想在http响应后显示,我应该怎么做?

这里有几个选项:

  1. 您可以将调度(从react组件(传递到该函数中并调用它。

  2. 由于您在此处导入了存储,因此可以删除constdispatch=useDispatch((,并使用store.dispatch({type:'SUBMIT_DECISION',payload:1}(

最新更新