ResourceCacheContext
The context through which useResource reaches the renderer's ResourceCache.
The context through which useResource reaches the renderer's ResourceCache. Server
renderers and hydration provide it; the value is null when resources should always fetch. Only
a custom renderer needs to provide it.
Import#
ts
import { ResourceCacheContext } from "@shadow-js/core";
Signature#
ts
const ResourceCacheContext: Context<ResourceCache | null>
Examples#
ts
import { type ResourceCache, ResourceCacheContext } from "@shadow-js/core";
const values = new Map<string, unknown>();
let created = 0;
const cache: ResourceCache = {
key: (explicit) => explicit ?? `resource-${created++}`,
read: (key) => (values.has(key) ? { value: values.get(key) } : undefined),
track: (key, promise) => void promise.then((value) => values.set(key, value)),
};
const page = <ResourceCacheContext value={cache}><p>Loading data</p></ResourceCacheContext>;