UseAxios
guarahooks/use-axiosFreeHook
A customizable hook for making HTTP requests with Axios
Install it
npx shadcn@latest add https://guarahooks.com/r/use-axios.jsonSource
1'use client';23import {4 createContext,5 useCallback,6 useContext,7 useEffect,8 useRef,9 useState,10 type ReactNode,11} from 'react';1213import axios, {14 AxiosError,15 AxiosInstance,16 AxiosRequestConfig,17 AxiosResponse,18} from 'axios';1920// #region Types & Interfaces2122export interface AxiosConfig extends AxiosRequestConfig {23 retries?: number;24 retryDelay?: number;25}2627export interface AxiosOptions extends AxiosRequestConfig {28 retries?: number;29 retryDelay?: number;30 immediate?: boolean;31}3233export interface AxiosResult<T> {34 data: T | null;35 error: AxiosError | null;36 loading: boolean;37 refetch: () => Promise<T | null>;38 abort: () => void;39 aborted: boolean;40}4142export interface AxiosContextValue {43 config: AxiosConfig;44 updateConfig: (newConfig: Partial<AxiosConfig>) => void;45 instance: AxiosInstance;46 request: <T = any>(config: AxiosRequestConfig) => Promise<AxiosResponse<T>>;47}4849export interface AxiosProviderProps {50 config?: AxiosConfig;51 children: ReactNode;52}5354// #endregion5556const AxiosContext = createContext<AxiosContextValue | null>(null);5758export function AxiosProvider({ config = {}, children }: AxiosProviderProps) {59 const [currentConfig, setCurrentConfig] = useState<AxiosConfig>(config);60 const instanceRef = useRef<AxiosInstance | null>(axios.create(config));6162 const updateConfig = useCallback((newConfig: Partial<AxiosConfig>) => {63 setCurrentConfig((prev) => ({ ...prev, ...newConfig }));64 }, []);6566 // Create or update axios instance when config changes67 useEffect(() => {68 const { retries, retryDelay, ...axiosConfig } = currentConfig;6970 instanceRef.current = axios.create(axiosConfig);7172 // Add request interceptor for retry logic73 instanceRef.current.interceptors.request.use(74 (config) => config,75 (error) => Promise.reject(error),76 );7778 // Add response interceptor for error handling79 instanceRef.current.interceptors.response.use(80 (response) => response,81 (error) => Promise.reject(error),82 );8384 return () => {85 instanceRef.current = null;86 };87 }, [currentConfig]);8889 const enhancedRequest = useCallback(90 async <T = any,>(91 requestConfig: AxiosRequestConfig,92 ): Promise<AxiosResponse<T>> => {93 const {94 retries = currentConfig.retries ?? 0,95 retryDelay = currentConfig.retryDelay ?? 1000,96 ...axiosConfig97 } = requestConfig as AxiosRequestConfig & {98 retries?: number;99 retryDelay?: number;100 };101102// … truncated
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseArrayStateuse-array-state | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseBetterAuthuse-better-auth | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps | |
| UseCopyToClipboarduse-copy-to-clipboard | guarahooks | Hooks | Free | no deps |
