useCache
useCache
Introduction
This hook allows us to manipulate the passed command’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 command 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(command, options)
{
deepCompare: boolean | typeof isEqual;
dependencyTracking: boolean;
initialData: ClientResponseType<Response, Error> | null;
}
Returns
Returned values from this hook.
const values = useCache(command);
{
isFocused: boolean;
isOnline: boolean;
setFocused: (isFocused: boolean) => void;
setOnline: (isOnline: boolean) => void;
}