π‘Axios API Calls
Mock API calls
Axios API call using SWR
export function useGetRelatedProducts(id: string) {
const URL = [endpoints.related, { id, endpoints: 'products' }];
const { data, isLoading, error, isValidating } = useSWR(URL, fetcherPost, {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false
});
const memoizedValue = useMemo(
() => ({
relatedProducts: data as Products[],
relatedProductsLoading: isLoading,
relatedProductsError: error,
relatedProductsValidating: isValidating,
relatedProductsEmpty: !isLoading && !data?.length
}),
[data, error, isLoading, isValidating]
);
return memoizedValue;
}Set the default axios baseURL for calling the API
Example 1: With baseUrl
Example 2: Without baseUrl
Last updated