Skip to main content
Version: 4.x.x

useCache


Introduction

This hook allows us to manipulate the passed request’s Cache and receive change callbacks. It is important to make sure that cacheKey is not affected by auto-generation – that there is a stable connection between the hook's request and the cache.

const { data, error, loading, revalidate, onSuccess, onError, onFinished } = useCache(getUsers);

onSuccess((payload) => {
console.log(payload); // [ User, User, User ]
});

onError((error) => {
console.log(error); // { message: string }
});

onFinished(([payload, error, status]) => {
console.log(payload); // [ User, User, User ] | null
console.log(error); // { message: string } | null
console.log(status); // 200 / 400 / 404 / 500 ...
});

Options

Configuration options for this hook provided as a second parameter.

const { ... } = useCache(request, options)

{
deepCompare: boolean | typeof isEqual;
dependencyTracking: boolean;
initialData: {
...params1: ResponseReturnType<Response, Error, Adapter>;
...params2: ResponseDetailsType;
cacheTime: number;
clearKey: string;
garbageCollection: number;
}[data] | null;
}


Returns

Returned values from this hook.

const values = useCache(request);

{
isFocused: boolean;
isOnline: boolean;
setFocused: (isFocused: boolean) => void;
setOnline: (isOnline: boolean) => void;
}