📚 AtmoSwing Web Viewer Docs

← Back to Home

Hooks

Files


hooks/useCachedRequest

Generic cached request hook that provides shared in-memory caching across hook instances.

hooks/useCachedRequest.useCachedRequest(key, fetchFn, deps, options) ⇒ Object | * | boolean | Error | null | function | boolean

Custom hook for making cached API requests with automatic deduplication.

Kind: static method of hooks/useCachedRequest
Returns: Object - Request state object* - returns.data - The fetched databoolean - returns.loading - Whether the request is in progressError | null - returns.error - Error object if request failedfunction - returns.refresh - Function to force refetch bypassing cacheboolean - returns.fromCache - Whether data was served from cache

Param Type Default Description
key string | null Unique cache key. If null/undefined, fetch is skipped
fetchFn function Async function that returns the data
deps Array Dependency array for the effect
options Object Configuration options
[options.enabled] boolean true Whether the request is enabled
[options.initialData] * Initial data value
[options.ttlMs] number | null Time-to-live in milliseconds. If provided and cache is stale, refetch occurs

Example

const { data, loading, error, refresh } = useCachedRequest(
  'forecast-entities',
  () => getEntities(region, date, methodId, configId),
  [region, date, methodId, configId],
  { ttlMs: 60000 }
);

hooks/useCachedRequest~GLOBAL_CACHE : Map.<string, {timestamp: number, data: any}>

Global cache storage. Maps cache keys to objects containing timestamp and data.

Kind: inner constant of hooks/useCachedRequest