Skip to content

ISwr.Use<T> method (1 of 3)

Returns cached data immediately (if available) and revalidates in the background using the provided fetcher. The caller owns how data is retrieved — SWR only manages the cache.

public SwrResult<T> Use<T>(string? key, Func<string, Task<T?>> fetcher, SwrOptions? options = null,
Action? onChange = null)
parameterdescription
TThe type of data to fetch and cache.
keyThe cache key identifying this data.
fetcherA function that fetches fresh data given the cache key.
optionsPer-request override of global SWR options, or null to use defaults.
onChangeOptional callback invoked when the result changes (initial fetch or revalidation).

A SwrResult that updates asynchronously as data flows through cache hit, fetch, and revalidation.


Returns cached data for a composite key made of a URL and one additional argument. The tuple parts form the cache key (so different argument values yield separate cache entries) and are passed individually to the fetcher.

public SwrResult<T> Use<T, TArg>((string?, TArg) key, Func<string, TArg, Task<T?>> fetcher,
SwrOptions? options = null, Action? onChange = null)
parameterdescription
TThe type of data to fetch and cache.
TArgThe type of the additional key argument.
keyA tuple of (url, arg) that defines the composite cache key.
fetcherA function that receives the URL and argument separately.
optionsPer-request override of global SWR options, or null to use defaults.
onChangeOptional callback invoked when the result changes.

A SwrResult that updates asynchronously.


Returns cached data for a composite key made of a URL and two additional arguments. The tuple parts form the cache key and are passed individually to the fetcher.

public SwrResult<T> Use<T, TArg1, TArg2>((string?, TArg1, TArg2) key,
Func<string, TArg1, TArg2, Task<T?>> fetcher, SwrOptions? options = null,
Action? onChange = null)
parameterdescription
TThe type of data to fetch and cache.
TArg1The type of the first additional key argument.
TArg2The type of the second additional key argument.
keyA tuple of (url, arg1, arg2) that defines the composite cache key.
fetcherA function that receives the URL and both arguments separately.
optionsPer-request override of global SWR options, or null to use defaults.
onChangeOptional callback invoked when the result changes.

A SwrResult that updates asynchronously.