useSuspenseAtomicProp

function
react/src/hooks/suspense.ts:177

Overload 1

signature
useSuspenseAtomicProp<R, S, P, T>(storeSpec, options): T

Suspense-compatible version of useAtomicProp that throws a promise while loading. Subscribes to a single dotted path and calls options.load to produce the resolved value. While the promise is pending, React Suspense catches it and renders the nearest <Suspense> fallback.

Type parameters

NameTypeDescription
Rextends stringReducer name union.
Sextends Record<R, any>State record keyed by R.
Pextends stringDotted path within S[R].
TResolved value type.

Parameters

storeSpec — object with:

NameTypeDescription
propertyP
reducerR
NameTypeDescription
optionsSuspenseAtomicPropOptions<T, S>Loading options (see SuspenseAtomicPropOptions).

Returns

TThe resolved value of type T.

Example

example
function UserName({ userId }: { userId: string }) {
  const name = useSuspenseAtomicProp(
    { reducer: 'users', property: `byId.${userId}.name` },
    { load: async (name) => name ?? (await fetchUser(userId)).name },
  );
  return <span>{name}</span>;
}

// Wrap with Suspense
<Suspense fallback={<Spinner />}>
  <UserName userId="123" />
</Suspense>

Overload 2

signature
useSuspenseAtomicProp<R, S, T>(storeSpec, options): T

Suspense-compatible version of useAtomicProp that throws a promise while loading. Subscribes to a single dotted path and calls options.load to produce the resolved value. While the promise is pending, React Suspense catches it and renders the nearest <Suspense> fallback.

Type parameters

NameTypeDescription
Rextends stringReducer name union.
Sextends Record<R, any>State record keyed by R.
TResolved value type.

Parameters

storeSpec — object with:

NameTypeDescription
propertystring
reducerR
NameTypeDescription
optionsSuspenseAtomicPropOptions<T, S>Loading options (see SuspenseAtomicPropOptions).

Returns

TThe resolved value of type T.

Example

example
function UserName({ userId }: { userId: string }) {
  const name = useSuspenseAtomicProp(
    { reducer: 'users', property: `byId.${userId}.name` },
    { load: async (name) => name ?? (await fetchUser(userId)).name },
  );
  return <span>{name}</span>;
}

// Wrap with Suspense
<Suspense fallback={<Spinner />}>
  <UserName userId="123" />
</Suspense>