Axios API Calls

Mock API calls

Axios API call using SWR

  1. Address values have been initialised using useSWR with Axios fetcher like below:

src/states/menu.ts
import { useMemo } from 'react';

// third party
import useSWR, { mutate } from 'swr';

// types
import { MenuProps } from 'types/menu';

const initialState: MenuProps = {
  openedItem: '',
  isDashboardDrawerOpened: false
};

export const endpoints = {
  key: 'api/menu',
  master: 'master'
};

export function useGetMenuMaster() {
  // to fetch initial state based on endpoints

  const { data, isLoading } = useSWR(endpoints.key + endpoints.master, () => initialState, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });

  const memoizedValue = useMemo(
    () => ({
      menuMaster: data ?? (initialState as MenuProps),
      menuMasterLoading: isLoading
    }),
    [data, isLoading]
  );

  return memoizedValue;
}

export function handlerDrawerOpen(isDashboardDrawerOpened: boolean) {
  // to update `isDashboardDrawerOpened` local state based on key

  mutate(
    endpoints.key + endpoints.master,
    (currentMenuMaster = initialState) => {
      return { ...currentMenuMaster, isDashboardDrawerOpened };
    },
    false
  );
}

export function handlerActiveItem(openedItem: string) {
  // to update `openedItem` local state based on key

  mutate(
    endpoints.key + endpoints.master,
    (currentMenuMaster = initialState) => {
      return { ...currentMenuMaster, openedItem };
    },
    false
  );
}

Set the default axios baseURL for calling the API

Open the .env file and edit VITE_APP_API_URL.

Example: With BaseUrl

Last updated

Was this helpful?