Interface PublicConfiguration<Data, Error, Fn>

interface PublicConfiguration<
    Data = any,
    Error = any,
    Fn extends Fetcher = BareFetcher,
> {
    errorRetryInterval: number;
    errorRetryCount?: number;
    loadingTimeout: number;
    focusThrottleInterval: number;
    dedupingInterval: number;
    refreshInterval?: number | (latestData: undefined | Data) => number;
    refreshWhenHidden?: boolean;
    refreshWhenOffline?: boolean;
    revalidateOnFocus: boolean;
    revalidateOnReconnect: boolean;
    revalidateOnMount?: boolean;
    revalidateIfStale: boolean;
    shouldRetryOnError: boolean | (err: Error) => boolean;
    keepPreviousData?: boolean;
    suspense?: boolean;
    fallbackData?: Data | Promise<Data>;
    fetcher?: Fn;
    use?: Middleware[];
    fallback: { [key: string]: any };
    isPaused: () => boolean;
    onLoadingSlow: (
        key: string,
        config: Readonly<PublicConfiguration<Data, Error, Fn>>,
    ) => void;
    onSuccess: (
        data: Data,
        key: string,
        config: Readonly<PublicConfiguration<Data, Error, Fn>>,
    ) => void;
    onError: (
        err: Error,
        key: string,
        config: Readonly<PublicConfiguration<Data, Error, Fn>>,
    ) => void;
    onErrorRetry: (
        err: Error,
        key: string,
        config: Readonly<PublicConfiguration<Data, Error, Fn>>,
        revalidate: Revalidator,
        revalidateOpts: Required<RevalidatorOptions>,
    ) => void;
    onDiscarded: (key: string) => void;
    compare: (a: undefined | Data, b: undefined | Data) => boolean;
    isOnline: () => boolean;
    isVisible: () => boolean;
}

Type Parameters

Properties

errorRetryInterval: number

error retry interval in milliseconds

5000
errorRetryCount?: number

max error retry count

loadingTimeout: number

timeout to trigger the onLoadingSlow event in milliseconds

3000
focusThrottleInterval: number

only revalidate once during a time span in milliseconds

5000
dedupingInterval: number

dedupe requests with the same key in this time span in milliseconds

2000
refreshInterval?: number | (latestData: undefined | Data) => number
refreshWhenHidden?: boolean

polling when the window is invisible (if refreshInterval is enabled)

false
refreshWhenOffline?: boolean

polling when the browser is offline (determined by navigator.onLine)

revalidateOnFocus: boolean

automatically revalidate when window gets focused

true
revalidateOnReconnect: boolean

automatically revalidate when the browser regains a network connection (via navigator.onLine)

true
revalidateOnMount?: boolean

enable or disable automatic revalidation when component is mounted

revalidateIfStale: boolean

automatically revalidate even if there is stale data

shouldRetryOnError: boolean | (err: Error) => boolean

retry when fetcher has an error

true
keepPreviousData?: boolean

keep the previous result when key is changed but data is not ready

false
suspense?: boolean

enable React Suspense mode

false
fallbackData?: Data | Promise<Data>

initial data to be returned (note: This is per-hook)

fetcher?: Fn

the fetcher function

use?: Middleware[]

array of middleware functions

fallback: { [key: string]: any }

a key-value object of multiple fallback data

isPaused: () => boolean

function to detect whether pause revalidations, will ignore fetched data and errors when it returns true. Returns false by default.

onLoadingSlow: (
    key: string,
    config: Readonly<PublicConfiguration<Data, Error, Fn>>,
) => void

callback function when a request takes too long to load (see loadingTimeout)

onSuccess: (
    data: Data,
    key: string,
    config: Readonly<PublicConfiguration<Data, Error, Fn>>,
) => void

callback function when a request finishes successfully

onError: (
    err: Error,
    key: string,
    config: Readonly<PublicConfiguration<Data, Error, Fn>>,
) => void

callback function when a request returns an error

onErrorRetry: (
    err: Error,
    key: string,
    config: Readonly<PublicConfiguration<Data, Error, Fn>>,
    revalidate: Revalidator,
    revalidateOpts: Required<RevalidatorOptions>,
) => void

handler for error retry

onDiscarded: (key: string) => void

callback function when a request is ignored

compare: (a: undefined | Data, b: undefined | Data) => boolean

comparison function used to detect when returned data has changed, to avoid spurious rerenders. By default, stable-hash is used.

isOnline: () => boolean

isOnline and isVisible are functions that return a boolean, to determine if the application is "active". By default, SWR will bail out a revalidation if these conditions are not met.

isVisible: () => boolean

isOnline and isVisible are functions that return a boolean, to determine if the application is "active". By default, SWR will bail out a revalidation if these conditions are not met.