Skip to Content

useOsdkObject

Load one object by primary key, or observe an object instance you already have, from the shared cache.

import { useOsdkObject } from "@osdk/react";

function useOsdkObject<Q extends ObjectOrInterfaceDefinition>(
  type: Q,
  primaryKey: PrimaryKeyType<Q>,
  options?: UseOsdkObjectOptions<Q>,
): UseOsdkObjectResult<Q>;

function useOsdkObject<Q extends ObjectOrInterfaceDefinition>(
  type: Q,
  primaryKey: PrimaryKeyType<Q>,
  enabled?: boolean,
): UseOsdkObjectResult<Q>;

function useOsdkObject<Q extends ObjectOrInterfaceDefinition>(
  obj: Osdk.Instance<Q>,
  enabled?: boolean,
): UseOsdkObjectResult<Q>;

Pass a type and primary key to fetch the object, or pass an Osdk.Instance to observe it in the cache without a network request. The component re-renders when the cached object changes, for example after an action or an optimistic update.

Parameters

ParameterTypeDefaultDescription
typeQThe object type or interface from your generated SDK.
primaryKeyPrimaryKeyType<Q>The primary key of the object to load.
objOsdk.Instance<Q>An object you already loaded. The hook reads it from the cache only and does not fetch.
enabledbooleantrueWhen false, the hook does not subscribe or fetch, and object is undefined.
optionsUseOsdkObjectOptions<Q>See below.

Options

The options type has little JSDoc, so this table is written by hand.

OptionTypeDefaultDescription
$selectreadonly PropertyKeys<Q>[]All propertiesProperties to load.
enabledbooleantrueWhen false, the hook does not subscribe or fetch, and object is undefined.
$loadPropertySecurityMetadatabooleanfalseLoads per-property marking requirements into $propertySecurities.
$includeAllBaseObjectPropertiesbooleanfalseFor an interface, loads every property of the underlying object type. No effect for object types.

Returns

FieldTypeDescription
objectOsdk.Instance<Q> | undefinedThe object, or undefined before it loads, on error, or while disabled.
isLoadingbooleantrue while enabled and the object has not loaded. false once error is set.
errorError | undefinedThe error from the last load, if any.
isOptimisticbooleantrue while the cached object reflects an optimistic update that the server has not confirmed.
forceUpdate() => voidNot implemented. Calling it throws.

Example

src/EmployeeCard.tsx
import { Employee } from "@my-app/sdk";
import { useOsdkObject } from "@osdk/react";

export function EmployeeCard({ employeeId }: { employeeId: number }) {
  const { object: employee, isLoading, error } = useOsdkObject(
    Employee,
    employeeId,
    { $select: ["fullName", "jobTitle"] },
  );

  return (
    <article aria-busy={isLoading}>
      {error && <p role="alert">{error.message}</p>}
      <h2>{employee?.fullName}</h2>
      <p>{employee?.jobTitle}</p>
    </article>
  );
}

See also

On this page