|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "ListLiveUpdate" |
| 4 | +--- |
| 5 | + |
| 6 | +# `<ListLiveUpdate>` |
| 7 | + |
| 8 | +`<ListLiveUpdate>` is an [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon" src="./img/premium.svg" /> component that refreshes its parent `ListContext` (e.g in a [`<List>`](./List.md)) when a record is created, updated, or deleted. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +Add the `<ListLiveUpdate>` in your `<List>` children: |
| 15 | + |
| 16 | +```jsx |
| 17 | +import { Datagrid, List TextField } from 'react-admin'; |
| 18 | +import { ListLiveUpdate } from '@react-admin/ra-realtime'; |
| 19 | + |
| 20 | +const PostList = () => ( |
| 21 | + <List> |
| 22 | + <Datagrid> |
| 23 | + <TextField source="title" /> |
| 24 | + </Datagrid> |
| 25 | + <ListLiveUpdate /> |
| 26 | + </List> |
| 27 | +); |
| 28 | +``` |
| 29 | + |
| 30 | +To trigger refreshes of `<ListLiveUpdate>`, the API has to publish events containing at least the followings: |
| 31 | + |
| 32 | +```js |
| 33 | +{ |
| 34 | + topic : '/resource/{resource}', |
| 35 | + event: { |
| 36 | + type: '{deleted || created || updated}', |
| 37 | + payload: { ids: [{listOfRecordIdentifiers}]}, |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +This also works with [`<ReferenceManyField>`](https://marmelab.com/react-admin/ReferenceManyField.html) or [`<ReferenceArrayField>`](https://marmelab.com/react-admin/ReferenceArrayField.html): |
| 44 | +```tsx |
| 45 | +import { Show, SimpleShowLayout, ReferenceManyField, Datagrid, TextField, DateField } from 'react-admin'; |
| 46 | +import { ListLiveUpdate } from '@react-admin/ra-realtime'; |
| 47 | +const AuthorShow = () => ( |
| 48 | + <Show> |
| 49 | + <SimpleShowLayout> |
| 50 | + <TextField source="first_name" /> |
| 51 | + <TextField source="last_name" /> |
| 52 | + <ReferenceManyField reference="books" target="author_id" label="Books"> |
| 53 | + <Datagrid> |
| 54 | + <TextField source="title" /> |
| 55 | + <DateField source="published_at" /> |
| 56 | + </Datagrid> |
| 57 | + <ListLiveUpdate /> |
| 58 | + </ReferenceManyField> |
| 59 | + </SimpleShowLayout> |
| 60 | + </Show> |
| 61 | +); |
| 62 | +``` |
| 63 | + |
| 64 | +## `onEventReceived` |
| 65 | + |
| 66 | +The `<ListLiveUpdate>` allows you to customize the side effects triggered when it receives a new event, by passing a function to the `onEventReceived` prop: |
| 67 | + |
| 68 | +```jsx |
| 69 | +import { Datagrid, List, TextField, useNotify, useRefresh } from 'react-admin'; |
| 70 | +import { ListLiveUpdate } from '@react-admin/ra-realtime'; |
| 71 | + |
| 72 | +const PostList = () => { |
| 73 | + const notify = useNotify(); |
| 74 | + const refresh = useRefresh(); |
| 75 | + |
| 76 | + const handleEventReceived = event => { |
| 77 | + const count = get(event, 'payload.ids.length', 1); |
| 78 | + notify(`${count} items updated by another user`); |
| 79 | + refresh(); |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <List> |
| 84 | + <Datagrid> |
| 85 | + <TextField source="title" /> |
| 86 | + </Datagrid> |
| 87 | + <ListLiveUpdate onEventReceived={handleEventReceived} /> |
| 88 | + </List> |
| 89 | + ); |
| 90 | +}; |
| 91 | +``` |
0 commit comments