π‘Axios API Calls
Mock API calls
Axios API call using SWR
Product values have been initialized using useSWR with Axios fetcher, like below:
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;
}You can mutate the product list with an Axios call, like below,
Set the default axios baseURL for calling the API
Open next.config.js file and edit NEXT_APP_API_URL.
Axios has been configured in the folder src\utils\axios
Example 1: With baseUrl
Example 2: Without baseUrl
You can set the entire URL in the Axios request. Do not use common Axios instances src\utils\axios instead, use the Axios library.
Last updated