@void-snippets/react

useGet

Single-item query hook — auto-disabled when id is empty.

#What it does

Fetches a single item by ID. Automatically disables itself when the ID is absent — no conditional hook calls, no if (id) guards, no stale data flashes.

#Signature

typescript
contactHooks.useGet(
  id:        Contact.Id | undefined | null | '',
  staleTime?: number,
): VSUseGetReturn<Contact.Detail>

#Parameters

ParameterTypeDefaultDescription
idTId | undefined | null | ''requiredThe item's ID. Query is disabled automatically when this is falsy.
staleTimenumber (ms)30_000How long the cached result stays fresh before a background refetch.

#Return values

FieldTypeDescription
itemTDetail | undefinedThe fetched item. undefined while loading or when the query is disabled.
isLoadingbooleantrue on first fetch when there is no cached data.
isFetchingbooleantrue during any network request for this item.
isRefetchingbooleantrue during a background refetch while item is already showing.
isErrorbooleantrue when the last fetch failed.
errorError | nullThe error from the last failed fetch.
refetchfunctionManually re-fetch this item.

#Example

tsx
function ContactDetailPage() {
  const { contactId } = useParams();

  // useGet stays disabled until contactId is defined
  const { item: contact, isLoading, isError, error, refetch } = contactHooks.useGet(
    contactId ? stringToId<Contact.Id>(contactId) : undefined,
  );

  if (isLoading) return <DetailSkeleton />;
  if (isError)   return <ErrorBanner message={error?.message} onRetry={refetch} />;
  if (!contact)  return null;

  return (
    <div>
      <h1>{contact.name}</h1>
      <p>{contact.email}</p>
      <p>Created by: {contact.createdBy.name}</p>
      <p>Notes: {contact.notes}</p>
    </div>
  );
}