Back to React Admin

WithLocks

docs_headless/src/content/docs/WithLocks.md

5.14.61.5 KB
Original Source

<WithLocks> fetches the locks for a resource on mount, and puts them in a LocksContext. The locks are updated in real time.

This component calls dataProvider.getLocks(), then subscribes to the locks topic for the current resource, and refetches the locks when a new event is received.

This feature requires a valid Enterprise Edition subscription.

Installation

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

Usage

tsx
import { ListBase, useRecordContext } from 'ra-core';
import { WithLocks, useLocksContext } from '@react-admin/ra-core-ee';
import { DataTable } from 'your-ra-ui-library';

const LockField = () => {
    const locks = useLocksContext();
    const record = useRecordContext();

    if (!record) return null;

    const lock = locks.find(lock => lock.recordId === record?.id);
    if (!lock) return null;

    return <span>Locked by {lock.identity}</span>;
};

const PostList = () => (
    <WithLocks>
        <ListBase>
            <DataTable>
                <DataTable.Col source="lockStatus" field={LockField} />
            </DataTable>
        </ListBase>
    </WithLocks>
);

Props

PropRequiredTypeDefaultDescription
childrenRequiredReactNodeThe component to render inside the LocksContext