useLock

useLock returns a lock function for acquiring a record lock via the LockProvider configured on the data provider.

Usage

import { useLock } from "@/components/realtime";
 
function LockButton({ resource, id }: { resource: string; id: number }) {
  const { lock, isLoading, error } = useLock();
 
  const handleLock = async () => {
    try {
      await lock(resource, { id });
      console.log("locked!");
    } catch (e) {
      console.error("lock failed", e);
    }
  };
 
  return (
    <button onClick={handleLock} disabled={isLoading}>
      Lock
    </button>
  );
}

Params

None.

Returns

FieldTypeDescription
lock(resource, params) => Promise<Lock>Acquires a lock
isLoadingbooleantrue while the lock call is in flight or identity is loading
errorError | nullThe last lock error

lock signature

lock(
  resource: string,
  params: { id: Identifier; identity?: Identifier; meta?: Record<string, unknown> }
): Promise<Lock>

Notes

  • If identity is omitted from params, the hook falls back to useGetIdentity() from the auth provider. If no identity is available from either source, the call throws.
  • Throws LockConflictError (with .existingLock) when another user holds the lock.
  • Must be used inside an <Admin> or CoreAdminContext. Throws immediately at render time if no DataProvider is found.
  • For automatic lock-on-mount behaviour, use useLockOnMount or <LockOnMount> instead.