Skip to content

useLockCallbacks

This utility hook allows to easily get the callbacks to lock and unlock a record, as well as the current lock status.

This feature requires a valid Enterprise Edition subscription.

Terminal window
npm install --save @react-admin/ra-core-ee
# or
yarn add @react-admin/ra-core-ee

Use this hook e.g. to build a lock button:

import { useLockCallbacks } from '@react-admin/ra-core-ee';
import { LoaderCircle, Lock } from 'lucide-react';
export const LockButton = () => {
const {
lock,
isLocked,
isLockedByCurrentUser,
isPending,
isLocking,
isUnlocking,
doLock,
doUnlock,
} = useLockCallbacks();
if (isPending) {
return null;
}
return isLocked ? (
isLockedByCurrentUser ? (
<button
disabled={isUnlocking}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
doUnlock();
}}
title="Locked by you, click to unlock"
>
{isUnlocking ? (
<LoaderCircle className="w-4 h-4 animate-spin" />
) : (
<Lock className="w-4 h-4" />
)}
</button>
) : (
<Lock title={`Locked by another user: ${lock?.identity}`} />
)
) : (
<button
disabled={isLocking}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
doLock();
}}
title="Record is unlocked, click to lock"
>
{isLocking ? <LoaderCircle className="w-4 h-4 animate-spin" /> : <LockOpenIcon className="w-4 h-4" />}
</button>
);
};

You can also leverage this hook as a quick way to access the lock status of the current record:

import { useLockCallbacks } from '@react-admin/ra-core-ee';
export const MyToolbar = () => {
const { isLockedByCurrentUser } = useLockCallbacks();
return (
<div>
<button type="submit" disabled={!isLockedByCurrentUser}>Save</button>
</div>
);
};

useLockCallbacks accepts a single options parameter, with the following properties:

NameRequiredTypeDefault ValueDescription
identityNoIdentifierFrom AuthProvider.getIdentity()An identifier for the user who owns the lock.
resourceNostringFrom ResourceContextThe resource name (e.g. 'posts').
idNoIdentifierFrom RecordContextThe record id (e.g. 123).
metaNoobject-Additional metadata forwarded to the dataProvider lock(), unlock() and getLock() calls.
lockMutationOptionsNoobject-react-query mutation options, used to customize the lock side-effects.
unlockMutationOptionsNoobject-react-query mutation options, used to customize the unlock side-effects.
queryOptionsNoobject-react-query query options, used to customize the lock query side-effects.

You can call useLockCallbacks with no parameter, and it will guess the resource and record id from the context (or the route):

const { isLocked, error, isLocking } = useLockCallbacks();

Or you can provide them explicitly:

const { isLocked, error, isLocking } = useLockCallbacks({
resource: 'venues',
id: 123,
identity: 'John Doe',
});

useLockCallbacks returns an object with the following properties:

NameTypeDescription
isLockedbooleanWhether the record is currently locked (possibly by another user) or not.
isLockedByCurrentUserbooleanWhether the record is locked by the current user or not.
lockobjectThe lock data.
errorobjectThe error object if any of the mutations or the query fails.
isPendingbooleanWhether the lock query is in progress.
isLockingbooleanWhether the lock mutation is in progress.
isUnlockingbooleanWhether the unlock mutation is in progress.
doLockfunctionA callback to manually lock the record.
doUnlockfunctionA callback to manually unlock the record.
doLockAsyncfunctionA callback to manually lock the record asynchronously.
doUnlockAsyncfunctionA callback to manually unlock the record asynchronously.
lockQueryobjectThe react-query query object for the lock status.
lockMutationobjectThe react-query mutation object for the lock mutation.
unlockMutationobjectThe react-query mutation object for the unlock mutation.