API Options
const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)
Parameters
key
: a unique key string for the request (or a function / array / null) (advanced usage)fetcher
: (optional) a Promise returning function to fetch your data (details)options
: (optional) an object of options for this SWR hook
Return Values
data
: data for the given key resolved byfetcher
(or undefined if not loaded)error
: error thrown byfetcher
(or undefined)isValidating
: if there’s a request or revalidation loadingmutate(data?, shouldRevalidate?)
: function to mutate the cached data (details)
Options
suspense = false
: enable React Suspense mode (details)fetcher(args)
: the fetcher functionrevalidateIfStale = true
: automatic revalidation on mount even if there is stale data (details)revalidateOnMount
: enable or disable automatic revalidation when component is mountedrevalidateOnFocus = true
: automatically revalidate when window gets focused (details)revalidateOnReconnect = true
: automatically revalidate when the browser regains a network connection (vianavigator.onLine
) (details)refreshInterval
(details):- Disabled by default:
refreshInterval = 0
- If set to a number, polling interval in milliseconds
- If set to a function, the function will receive the latest data and should return the interval in milliseconds
- Disabled by default:
refreshWhenHidden = false
: polling when the window is invisible (ifrefreshInterval
is enabled)refreshWhenOffline = false
: polling when the browser is offline (determined bynavigator.onLine
)shouldRetryOnError = true
: retry when fetcher has an errordedupingInterval = 2000
: dedupe requests with the same key in this time span in millisecondsfocusThrottleInterval = 5000
: only revalidate once during a time span in millisecondsloadingTimeout = 3000
: timeout to trigger the onLoadingSlow event in millisecondserrorRetryInterval = 5000
: error retry interval in millisecondserrorRetryCount
: max error retry countfallback
: a key-value object of multiple fallback data (example)fallbackData
: initial data to be returned (note: This is per-hook)onLoadingSlow(key, config)
: callback function when a request takes too long to load (seeloadingTimeout
)onSuccess(data, key, config)
: callback function when a request finishes successfullyonError(err, key, config)
: callback function when a request returns an erroronErrorRetry(err, key, config, revalidate, revalidateOps)
: handler for error retrycompare(a, b)
: comparison function used to detect when returned data has changed, to avoid spurious rerenders. By default, dequal is used.isPaused()
: function to detect whether pause revalidations, will ignore fetched data and errors when it returnstrue
. Returnsfalse
by default.use
: array of middleware functions (details)
💡
When under a slow network (2G, <= 70Kbps), errorRetryInterval
will be
10s, and loadingTimeout
will be 5s by default.
You can also use global configuration to provide default options.