useInstance()function

useInstance(Constructor: new (...a: A) => T, ...args: A): T
ParamType
Constructornew (...a: A) => T
Class to instantiate with args. required
argsA
Constructor arguments — a change in args constructs a fresh instance. required
Return
T
The memoised instance, reconstructed only when args changes.

Use a memoised class instance.

  • Creates a new instance of Constructor using args
  • Returns same instance for as long as args is equal to previous args.

Create a stable class instance inside a component. A new instance is constructed only when the constructor arguments change (by deep equality of the argument list), so the instance is safe to depend on across renders.

This is useful for creating stores, caches, or other objects that should live for the lifetime of a component without being hoisted to module scope — for example when the object depends on props.

Usage

tsx
import { useInstance } from "shelving/react";
import { APICache } from "shelving/api";

function MyComponent({ provider }: { provider: APIProvider }) {
  const cache = useInstance(APICache, provider); // Stable across renders.
  // ...rebuilt only when `provider` changes.
}

Arguments are compared by deep equality, so passing equal-but-not-identical values (a fresh object literal each render) does not trigger reconstruction.

Examples

const cache = useInstance(DBCache, provider);