Skip to main content
Version: v8.0.0

useFetch

This hook aims to retrieve data from the server. It automatically fetches on mount and refetches based on dependencies, with support for caching, polling, and suspense.


Import
import { useFetch } from "@hyper-fetch/react"

Parameters​

useFetch<R, TSelected>(request: R, options: UseFetchOptionsType<R, TSelected>)
Parameters
NameTypeDescription
request
R

Request instance

options
UseFetchOptionsType<R, TSelected>

Hook options

Returns​

type UseFetchReturnType<R,TSelected> = UseTrackedStateType<R, TSelected> & UseTrackedStateActions<R, TSelected> & UseRequestEventsActionsType<R> & { bounce: { active: boolean; reset: () => void }; refetch: () => void };
useFetch
NameTypeDescription
data
null | TData

Request response data

error
null | ExtractErrorType<T>

Request response error

extra
null | ExtractAdapterExtraType<ExtractAdapterType<T>>

Request additional response data

loading
boolean

Request loading state

requestTimestamp
null | Date

Request response timestamp

responseTimestamp
null | Date

Request response timestamp

retries
number

Request attempts

status
null | ExtractAdapterStatusType<ExtractAdapterType<T>> | null

Request status

success
boolean

Information whether request succeeded

bounce
{ active: boolean; reset: () => void }

Data related to current state of the bounce usage

clearState
() => void

Reset all state fields to their initial values (data, error, status, extra → null, loading → false, etc.).

setData
(data: CacheSetState<TData | null>) => void

Action to set custom data. We can do it locally(inside hook state). When select is used it receives the selected data shape. If you need to update cache data use client.cache.update(). method.

setError
(error: CacheSetState<ExtractErrorType<T> | null>) => void

Action to set custom error. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setExtra
(extra: CacheSetState<ExtractAdapterExtraType<ExtractAdapterType<T>> | null>) => void

Action to set custom additional data. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setLoading
(loading: CacheSetState<boolean>) => void

Action to set custom loading. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setRequestTimestamp
(timestamp: CacheSetState<Date>) => void

Action to set custom timestamp. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setResponseTimestamp
(timestamp: CacheSetState<Date>) => void

Action to set custom timestamp. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setRetries
(retries: CacheSetState<number>) => void

Action to set custom retries count. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setStatus
(status: CacheSetState<ExtractAdapterStatusType<ExtractAdapterType<T>>>) => void

Action to set custom status. We can do it locally(inside hook state). If you need to turn on loading for all listening hooks use client.requestManager.events.emitLoading() method.

setSuccess
(success: CacheSetState<boolean>) => void

Action to set custom success. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

abort
() => void

Callback that allows canceling ongoing requests from the given queryKey.

onAbort
(callback: OnErrorCallbackType<T>) => void

Helper hook listening on aborting of requests. Includes mutationContext when setOptimistic is configured.

onDownloadProgress
(callback: OnProgressCallbackType) => void

Helper hook listening on download progress ETA.

onError
(callback: OnErrorCallbackType<T>) => void

Helper hook listening on error response. Includes mutationContext when setOptimistic is configured.

onFinished
(callback: OnFinishedCallbackType<T>) => void

Helper hook listening on any response. Includes mutationContext when setOptimistic is configured.

onOfflineError
(callback: OnErrorCallbackType<T>) => void

Helper hook listening on request going into offline awaiting for network connection to be restored.

onRequestStart
(callback: OnStartCallbackType<T>) => void

Helper hook listening on request start.

onResponseStart
(callback: OnStartCallbackType<T>) => void

Helper hook listening on response start(before we receive all data from server).

onSuccess
(callback: OnSuccessCallbackType<T>) => void

Helper hook listening on success response. Includes mutationContext when setOptimistic is configured.

onUploadProgress
(callback: OnProgressCallbackType) => void

Helper hook listening on upload progress ETA.

refetch
() => void

Refetch current request