|
| 1 | +--- |
| 2 | +title: 'ListLiveUpdate' |
| 3 | +--- |
| 4 | + |
| 5 | +`<ListLiveUpdate>` refreshes its parent `ListContext` (e.g in a [`<ListBase>`](./ListBase.md)) when a record is created, updated, or deleted. It therefore displays up-to-date data in real-time. |
| 6 | + |
| 7 | +This feature requires a valid is an [Enterprise Edition](https://react-admin-ee.marmelab.com) subscription. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @react-admin/ra-core-ee |
| 13 | +# or |
| 14 | +yarn add @react-admin/ra-core-ee |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Add the `<ListLiveUpdate>` in your `<ListBase>` children: |
| 20 | + |
| 21 | +```tsx {11} |
| 22 | +import { ListBase, RecordsIterator } from 'ra-core'; |
| 23 | +import { ListLiveUpdate } from '@react-admin/ra-core-ee'; |
| 24 | + |
| 25 | +const PostList = () => ( |
| 26 | + <ListBase> |
| 27 | + <ul> |
| 28 | + <RecordsIterator |
| 29 | + render={record => <li>{record.title} - {record.views}</li>} |
| 30 | + /> |
| 31 | + </ul> |
| 32 | + <ListLiveUpdate /> |
| 33 | + </ListBase> |
| 34 | +); |
| 35 | +``` |
| 36 | + |
| 37 | +To trigger a refresh of `<ListLiveUpdate>`, the API has to publish an event containing at least the following data: |
| 38 | + |
| 39 | +```js |
| 40 | +{ |
| 41 | + topic : '/resource/{resource}', |
| 42 | + event: { |
| 43 | + type: '{deleted || created || updated}', |
| 44 | + payload: { ids: [{listOfRecordIdentifiers}]}, |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +This also works with [`<ReferenceManyFieldBase>`](./ReferenceManyFieldBase.md) or [`<ReferenceArrayFieldBase>`](./ReferenceArrayFieldBase.md): |
| 50 | + |
| 51 | +```tsx |
| 52 | +import { ShowBase, RecordsIterator } from 'ra-core'; |
| 53 | +import { ReferenceManyFieldBase, ListLiveUpdate } from '@react-admin/ra-core-ee'; |
| 54 | + |
| 55 | +const AuthorShow = () => ( |
| 56 | + <ShowBase> |
| 57 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}> |
| 58 | + {/* Other author details here */} |
| 59 | + <ReferenceManyFieldBase |
| 60 | + reference="books" |
| 61 | + target="author_id" |
| 62 | + label="Books" |
| 63 | + > |
| 64 | + <ul> |
| 65 | + <RecordsIterator |
| 66 | + render={record => <li>{record.title}</li>} |
| 67 | + /> |
| 68 | + </ul> |
| 69 | + <ListLiveUpdate /> |
| 70 | + </ReferenceManyFieldBase> |
| 71 | + </div> |
| 72 | + </ShowBase> |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +## Props |
| 77 | + |
| 78 | +| Prop | Required | Type | Default | Description | |
| 79 | +| ----------------- | -------- | ---------- | ------- | -------------------------------------------------------------------------- | |
| 80 | +| `onEventReceived` | Optional | `function` | - | A function that allows to customize side effects when an event is received | |
| 81 | + |
| 82 | +## `onEventReceived` |
| 83 | + |
| 84 | +The `<ListLiveUpdate>` allows you to customize the side effects triggered when it receives a new event, by passing a function to the `onEventReceived` prop: |
| 85 | + |
| 86 | +```tsx |
| 87 | +import { ListBase, RecordsIterator, useNotify, useRefresh } from 'ra-core'; |
| 88 | +import { ReferenceManyFieldBase, ListLiveUpdate } from '@react-admin/ra-core-ee'; |
| 89 | + |
| 90 | +const PostList = () => { |
| 91 | + const notify = useNotify(); |
| 92 | + const refresh = useRefresh(); |
| 93 | + |
| 94 | + const handleEventReceived = (event) => { |
| 95 | + const count = get(event, 'payload.ids.length', 1); |
| 96 | + notify(`${count} items updated by another user`); |
| 97 | + refresh(); |
| 98 | + }; |
| 99 | + |
| 100 | + return ( |
| 101 | + <ListBase> |
| 102 | + <ul> |
| 103 | + <RecordsIterator |
| 104 | + render={record => <li>{record.title} - {record.views}</li>} |
| 105 | + /> |
| 106 | + </ul> |
| 107 | + <ListLiveUpdate onEventReceived={handleEventReceived} /> |
| 108 | + </ListBase> |
| 109 | + ); |
| 110 | +}; |
| 111 | +``` |
0 commit comments