Interface FetchOptions<T>

interface FetchOptions<T> {
    discardStaleRequests?: boolean;
    parseType?: ParseType;
    preFetchCallback?: ((setState: Dispatch<SetStateAction<undefined | T>>, newUrl: string) => boolean);
    requestOptions?: RequestInit;
    retryTimeout?: ((attempt: number) => undefined | number);
}

Type Parameters

  • T

Properties

discardStaleRequests?: boolean

Discards all responses except the last one made. This prevents flickering of data in the UI and ensures only the correct data is displayed. Only turn this off if you're really sure you want to.

parseType?: ParseType

How to parse the result (determines type of returned data). Set to "response" if you don't want to extract the data automatically and receive the response object instead.

preFetchCallback?: ((setState: Dispatch<SetStateAction<undefined | T>>, newUrl: string) => boolean)

This callback gets called before fetching. It gives you access to the setter function for the data and the url to fetch. The return value of this function is a boolean indicating whether to perform a fetch from the new url. The intended use for this hook is mostly to prevent fetching from URLs that you know are going to return a non-200 result. In that case you can keep the previous state or set some empty state depending on your use case. You can of course also just use this hook to execute some arbitrary code before fetching.

requestOptions?: RequestInit

The options passed to the fetch function. Note: custom signals will be overwritten internally.

retryTimeout?: ((attempt: number) => undefined | number)

A callback function that takes the number of attempted retries and returns the amount of seconds to wait before attempting to fetch again. A common retry method would return 2^(attempt). If set to undefined, does not attempt to re-fetch on failure. If the function returns undefined, it stops the hook from re-fetching again until an input parameter to the hook changes and resets the internal retry counter.