For the complete documentation index, see llms.txt. This page is also available as Markdown.

Axios API Calls

Mock API calls

Axios API call using SWR

Products values has been initialized using useSWR with Axios fetcher like below:

src/api/products.ts
export function useGetProducts() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.list, fetcher, {
    revalidateIfStale: true,
    revalidateOnFocus: true,
    revalidateOnReconnect: true
  });

  const memoizedValue = useMemo(
    () => ({
      products: data?.products,
      productsLoading: isLoading,
      productsError: error,
      productsValidating: isValidating,
      productsEmpty: !isLoading && !data?.products?.length
    }),
    [data, error, isLoading, isValidating]
  );

  return memoizedValue;
}

You can mutate products list with Axios call, like below,

Set default axios baseURL for call API

Open .env file and edit REACT_APP_API_URL.

You can configure the same for next.js as well.

Axios has been configured in the folder ..src\utils\axios.js

Example 1: With baseUrl

Example 2: Without baseUrl

You can set the entire URL in Axios request. Do not use common Axios instances src\utils\axios.jsinstead use directly Axios library.

Last updated