React hooks: useLoad, useListLoad, useLoading, useBoolean, useClickOutside, useCombinedRefs,
useCopyToClipboard, usePasswordGenerator, useUnloadConfirm, useScreenSize, useWindowScroll.
useLoad and useListLoad can keep loaded data between screen visits: cached data is shown instantly
and reloaded in the background (stale-while-revalidate), so returning to a screen shows no loader.
Caching is opt-in and works only when both are present:
LoadCacheProviderwith ascopeabove the hooks;cacheKeyon the hook.
Without the provider the hooks behave exactly as in 0.0.x — even if cacheKey is passed.
// scope = everything that changes server responses for the same request; null — cache off.
// Use a stable user id (not an access token) and the language.
<LoadCacheProvider scope={`${user?.id ?? "anon"}:${locale}`}>
<App />
</LoadCacheProvider>const { list, preloading } = useListLoad({
cacheKey: "websites",
count: 20,
load: ({ offset, count }) => api.getWebsites({ offset, count }),
});
const { data: website } = useLoad({
cacheKey: ["website", domain],
load: () => api.getWebsite(domain),
});- The full key is
scope + cacheKey + payload(useListLoadalso addscount), so different payloads never share data. - When
scopechanges (another account, logout, language) the cache is cleared. staleTime(ms, on the provider or the hook): data younger than this is not reloaded on mount. Default0— always reload in the background.useListLoadreloads the whole loaded list in one request (offset: 0, count: list.length), so the scroll position is kept.insertItem/replaceItem/removeItem/appendItem/prependItemupdate the cache too.nextLoad({ reset: true })reloads without clearing the list on screen.- Requests with the same key are shared; responses of outdated requests are ignored.
- Memory only: nothing is written to localStorage.
const { invalidate, clear } = useLoadCache();
// After saving data on another screen — mark it outdated:
await api.updateProduct(id, values);
invalidate("products"); // every key starting with "products" (any payload)
invalidate(["website", domain]);
clear();Outdated data is never shown: on the next visit the screen loads it as without cache (no flash of old
values). Hooks that are on screen at that moment reload in the background and keep what they show.
Call invalidate explicitly where data is saved; an extra call only costs one honest load.
Do not cache data that must never be shown stale: initial values of edit forms, payments.