簡體   English   中英

派遣時它顯示未捕獲的 RangeError:超出最大調用堆棧大小

[英]On dispatch it is showing me Uncaught RangeError: Maximum call stack size exceeded

我正在從 Navbar 組件發送注銷操作,但它顯示我每次單擊注銷按鈕時都超出了最大調用堆棧大小。

authSlice.js:

export const logout = createAsyncThunk("logout", async (action) => {
  localStorage.clear();
  return action;
});

導航欄.js

  const logout = () => {
    dispatch(logout());
    setUser(null);
  };

命名不明確引起的死循環

您有兩個功能稱為logout

const logout = () => {
    dispatch(logout()); // <-- calls itself
    setUser(null);
};

這里的第二行logout()沒有調用你的 thunk 動作創建者。 它在第一行調用函數。 然后執行第二行,調用第一行,執行第二行......直到達到最大調用堆棧大小。


確保將logout thunk 導入到組件文件中。

import { logout } from '../some/thunk/file';

並將您的事件處理程序重命名為其他名稱。

const handleLogout = () => {
    dispatch(logout()); // <-- calls the imported thunk
    setUser(null);
};

也可以在導入時重命名 thunk,如果您想這樣做的話。

import { logout as logoutAction } from '../some/thunk/file';
const logout = () => {
    dispatch(logoutAction()); // <-- calls the imported thunk
    setUser(null);
};

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM