簡體   English   中英

React Hook "useDispatch" 在 function "requestWithAction" 中被調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E683854F14ZA7

[英]React Hook "useDispatch" is called in function "requestWithAction" that is neither a React function component nor a custom React Hook function

當我使用 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 響應 function 中分散注意力? 如果我想在 http 響應后疏遠,我該怎么辦?

這里有幾個選項:

  1. 您可以將您的調度(來自一個反應組件)傳遞到這個 function 並調用它。

  2. 由於您在此處導入了商店,因此您可以刪除 const dispatch = useDispatch(),並使用 store.dispatch({ type: 'SUBMIT_DECISION', payload: 1})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 在既不是 React 函數組件也不是自定義 React Hook 函數的函數中調用 React Hook “useAxios” React Hook“useState”在 function“increaseCounter”中被調用,它既不是 React function 組件,也不是自定義 React Hook function React Hook "useForecast" 在 function "getSearch" 中調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385D1AB5074C17A94F React Hook "useRouter" 在 function "article" 中被調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385D1AB5074C17A94F1 React Hook "useAxios" 在 function "onFinish" 中被調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385D1AB5074C17ZA4 React Hook "useTranslation" 在 function "getMyMenu" 中被調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385D1AB5074C17A4 React Hook 在 function 中調用,它既不是 React function 組件,也不是自定義 React Hook function React Hook "React.useState" 在 function "form" 中調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385D1AB5074C17A94F React Hook “useReducer” 在 function “fetchData” 中被調用,它既不是 React function 組件也不是自定義 React Hook ZC1C425268E68385D1AB5074C17A9F React Hook 在 function “onSubmit” 中調用,它既不是 React function 組件也不是自定義 React Hook function
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM