RangeRor:最大呼叫堆栈尺寸超过了,React钩子

发布于 2025-02-09 17:38:44 字数 2672 浏览 2 评论 0原文

我正在尝试将React Native App(MOBX)转换为ReactJS应用程序,并且我想将React Hooks and Context(与LocalStorage一起)用于状态管理。我有一个主存储文件:

export const MainStore = () => {
  
  const AppContext = createContext();
  
  const initialState = {
     ...
  }
  
  const reducer = (state, action) => {
    return { ...state, ...action };
  }

  const [ initState, setInitState ] = useLocalStorage('state', initialState);
  
  const [ state, dispatch ] = useReducer(reducer, initState);

  const { getAssetData } = assets();

  const getInstMetaData = async () => {
    const params = {
      asset_type_name: "...",
      with: "..."
    };
    return getAssetData(params);
  };

  const checkForRefreshTokenUpdate = async () => {
      ...
  }

  return { 
    AppContext, 
    initialState, 
    state,
    dispatch,
    reducer, 
    checkForRefreshTokenUpdate,
    getInstMetaData
  };

我的资产文件是:

import { interceptedAxios } from "./axios/config";
import { buildUrl } from "./utils/urls";
import { getAccessToken, getBaseUrl } from "./index";


const assets = () => {

  const { customAxios } = interceptedAxios();
  /**
   * Fetches asset data.
   * @returns
   */
  const getAssetData = async (params) => {
    const url = buildUrl({
      baseUrl: getBaseUrl(),
      endpoint: "/api/asset",
      params
    });

    const reqOpts = {
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${getAccessToken()}`
      }
    };

    return customAxios.get(url, reqOpts).then(res => { res.data });
  };

  return { getAssetData }
}

export { assets };

我的截距:

import axios from "axios";
import { MainStore } from "../../stores/mainStore";
import { getBaseUrl } from "../index";


export const interceptedAxios = () => {
  // Create axios instance for customization and import this instead of "axios"
  const customAxios = axios.create();
  
  const { checkForRefreshTokenUpdate } = MainStore();
  
  customAxios.interceptors.request.use(async (config) => {

    if (config.url.includes(getBaseUrl()) && config.headers["Authorization"] != null) {
      await checkForRefreshTokenUpdate()
        .then((token) => {
          if (token != null) {
            config.headers["Authorization"] = `Bearer ${token}`;
          }
        });
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  });

  return { customAxios }
}

错误是:grangeRor:运行应用程序时,超过了最大呼叫堆栈大小。 我知道我在inreceptedaxios中使用的Mainstore()的错误。我正在制作类似依赖的圈子的东西,但是我想不出另一种方法,以某种方式我需要使用checkforrefreshtokenupdate。有什么想法吗?

I am trying to translate a react native app(MobX) to a reactJS app and I want to use react hooks and context(with localStorage) for state management. I have a mainStore file:

export const MainStore = () => {
  
  const AppContext = createContext();
  
  const initialState = {
     ...
  }
  
  const reducer = (state, action) => {
    return { ...state, ...action };
  }

  const [ initState, setInitState ] = useLocalStorage('state', initialState);
  
  const [ state, dispatch ] = useReducer(reducer, initState);

  const { getAssetData } = assets();

  const getInstMetaData = async () => {
    const params = {
      asset_type_name: "...",
      with: "..."
    };
    return getAssetData(params);
  };

  const checkForRefreshTokenUpdate = async () => {
      ...
  }

  return { 
    AppContext, 
    initialState, 
    state,
    dispatch,
    reducer, 
    checkForRefreshTokenUpdate,
    getInstMetaData
  };

My asset file is this:

import { interceptedAxios } from "./axios/config";
import { buildUrl } from "./utils/urls";
import { getAccessToken, getBaseUrl } from "./index";


const assets = () => {

  const { customAxios } = interceptedAxios();
  /**
   * Fetches asset data.
   * @returns
   */
  const getAssetData = async (params) => {
    const url = buildUrl({
      baseUrl: getBaseUrl(),
      endpoint: "/api/asset",
      params
    });

    const reqOpts = {
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${getAccessToken()}`
      }
    };

    return customAxios.get(url, reqOpts).then(res => { res.data });
  };

  return { getAssetData }
}

export { assets };

and my interceptedAxios:

import axios from "axios";
import { MainStore } from "../../stores/mainStore";
import { getBaseUrl } from "../index";


export const interceptedAxios = () => {
  // Create axios instance for customization and import this instead of "axios"
  const customAxios = axios.create();
  
  const { checkForRefreshTokenUpdate } = MainStore();
  
  customAxios.interceptors.request.use(async (config) => {

    if (config.url.includes(getBaseUrl()) && config.headers["Authorization"] != null) {
      await checkForRefreshTokenUpdate()
        .then((token) => {
          if (token != null) {
            config.headers["Authorization"] = `Bearer ${token}`;
          }
        });
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  });

  return { customAxios }
}

The error is: RangeError: Maximum call stack size exceeded, on running the app.
I know that the fault that is that I am using MainStore() in inreceptedAxios. I am making something like a circle in dependencies, but I can't think of another way to do it and somehow i need to use checkForRefreshTokenUpdate. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文