• Create a suspending function that will cache the result of the function call. This function will be called only once, with no arguments, and the result will be cached.

    Type Parameters

    Parameters

    • fetcher: (() => Promise<Value>)
        • (): Promise<Value>
        • The async function that will be called when suspending.

          If need to respond to different arguments, you can use bindKeyedSuspense instead.

          Returns Promise<Value>

    • options: {
          storage?: Storage;
      } = {}
      • Optional storage?: Storage

        (Advanced) You can provide the backing cache object

        By default it uses an internal implementation based on a simple variable, but you can provide your own implementation

    Returns {
        cache: Storage;
        suspend: (() => Value);
    }

    • cache: Storage

      Access to the backing cache.

      Remark

      Useful for doing cache.set(null), and force a re-fetch.

    • suspend: (() => Value)
        • (): Value
        • Suspend your tree while the async function resolves, and return its promise's value

          Returns Value

    Example

    import { bindSuspense } from "@cprecioso/react-suspense";

    const appConfig = bindSuspense(() =>
    fetch("/api/config").then((res) => res.json())
    );

    export const Greeting = () => {
    const { accentColor } = appConfig.suspend();
    return <h1 style={{ color: accentColor }}>Hello world</h1>;
    };

Generated using TypeDoc