UseFetch
guarahooks/use-fetchFreeHook
Abstracts the Fetch API with loading, error, and data state management.
Live preview
live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://guarahooks.com/r/use-fetch.jsonSource
1'use client';23import {4 createContext,5 useCallback,6 useContext,7 useEffect,8 useRef,9 useState,10 type PropsWithChildren,11 type ReactNode,12} from 'react';1314// #region Types & Interfaces15export interface FetchConfig extends Omit<RequestInit, 'body' | 'signal'> {16 baseURL?: string;17 timeout?: number;18 retries?: number;19 retryDelay?: number;20}2122export interface FetchOptions<TBody = BodyInit | null>23 extends Omit<RequestInit, 'body'> {24 body?: TBody;25 timeout?: number;26 retries?: number;27 retryDelay?: number;28 parse?: (response: Response) => Promise<any>;29}3031export interface FetchResult<T> {32 data: T | null;33 error: Error | null;34 loading: boolean;35 refetch: () => Promise<T | null>;36 abort: () => void;37 aborted: boolean;38}3940interface FetchContextValue {41 config: FetchConfig;42 updateConfig: (newConfig: Partial<FetchConfig>) => void;43 fetch: (url: string, options?: FetchOptions) => Promise<Response>;44}4546export interface FetchProviderProps {47 config?: FetchConfig;48 children: ReactNode;49}5051// #endregion5253const FetchContext = createContext<FetchContextValue | null>(null);5455export function FetchProvider({ config = {}, children }: FetchProviderProps) {56 const [currentConfig, setCurrentConfig] = useState<FetchConfig>(config);5758 const updateConfig = useCallback((newConfig: Partial<FetchConfig>) => {59 setCurrentConfig((prev) => ({ ...prev, ...newConfig }));60 }, []);6162 const enhancedFetch = useCallback(63 async (url: string, options: FetchOptions = {}) => {64 const {65 timeout = currentConfig.timeout,66 retries = currentConfig.retries ?? 0,67 retryDelay = currentConfig.retryDelay ?? 1000,68 ...fetchOptions69 } = options;7071 // Construct full URL72 const fullUrl =73 currentConfig.baseURL && !url.startsWith('http')74 ? `${currentConfig.baseURL.replace(/\/$/, '')}/${url.replace(/^\//, '')}`75 : url;7677 // Merge headers78 const headers = new Headers(currentConfig.headers);79 if (fetchOptions.headers) {80 const optionHeaders = new Headers(fetchOptions.headers);81 optionHeaders.forEach((value, key) => headers.set(key, value));82 }8384 // Create fetch options85 const finalOptions: RequestInit = {86 ...currentConfig,87 ...fetchOptions,88 headers,89 };9091 // Use provided signal or create new one92 const shouldCreateController = !fetchOptions.signal;93// … 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 | |
| UseAxiosuse-axios | 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 |
