Skip to main content
Version: 4.5

useResultCache()

function useResultCache(
fetchShape: ReadShape,
params: object | null,
defaults?: object,
): typeof defaults;
function useResultCache<Params extends Readonly<object>, D extends object>(
{ getFetchKey, fetch }: ReadShape<any, Params, any>,
params: Params | null,
defaults?: D,
): D extends undefined
? Resolved<ReturnType<typeof fetch>> | null
: Readonly<D>;

Rest Hooks 3.0 - Deprecation

This hook is being deprecated in favor of useCacheNew()

  • 3.0 useCacheNew() will be renamed to useCache()
  • 3.1 will remove useResultCache()

Excellent to use with pagination or any other extra (non-entity) data in results.

  • On Error (404, 500, etc):
    • Returns previously cached if exists
    • defaults if provided
    • null otherwise
  • While loading:
    • Returns previously cached if exists
    • defaults if provided
    • null otherwise

Example

By sending defaults we can destructure the values even if the results don't exist.

function PostList() {
const { prevPage, nextPage } = useResultCache(
PaginatedResource.listShape(),
{},
{ prevPage: '', nextPage: '' },
);
// ...render stuff here
}

Useful FetchShapes to send

Resource provides these built-in:

  • listShape()

Feel free to add your own FetchShape as well.