Skip to main content
Version: v7.0.0

useCache


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

Parameters

useCache<T>(request: T, options: UseCacheOptionsType<T>)
Parameters
NameTypeDescription
request
T
options
UseCacheOptionsType<T>

Returns

type UseCacheReturnType<T> = UseTrackedStateType<T> & UseTrackedStateActions<T> & { invalidate: (cacheKeys?: string | RegExp | RequestInstance | (string | RegExp | RequestInstance)[]) => void };
useCache
NameTypeDescription
data
null | ExtractResponseType<T>

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

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

Action to set custom data. We can do it locally(inside hook state). 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.

invalidate
(cacheKeys?: string | RegExp | RequestInstance | (string | RegExp | RequestInstance)[]) => void

Invalidate cache for the current request or pass custom key to trigger it by invalidationKey(Regex / cacheKey).

React
On this page