-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10531 from marmelab/doc-list-live-update
[Doc] Promote composition with `<ListLiveUpdate>` instead of `<ListLive>`
- Loading branch information
Showing
10 changed files
with
125 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
layout: default | ||
title: "ListLiveUpdate" | ||
--- | ||
|
||
# `<ListLiveUpdate>` | ||
|
||
`<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. | ||
|
||
 | ||
|
||
## Usage | ||
|
||
Add the `<ListLiveUpdate>` in your `<List>` children: | ||
|
||
```jsx | ||
import { Datagrid, List TextField } from 'react-admin'; | ||
import { ListLiveUpdate } from '@react-admin/ra-realtime'; | ||
|
||
const PostList = () => ( | ||
<List> | ||
<Datagrid> | ||
<TextField source="title" /> | ||
</Datagrid> | ||
<ListLiveUpdate /> | ||
</List> | ||
); | ||
``` | ||
|
||
To trigger refreshes of `<ListLiveUpdate>`, the API has to publish events containing at least the followings: | ||
|
||
```js | ||
{ | ||
topic : '/resource/{resource}', | ||
event: { | ||
type: '{deleted || created || updated}', | ||
payload: { ids: [{listOfRecordIdentifiers}]}, | ||
} | ||
} | ||
``` | ||
|
||
|
||
This also works with [`<ReferenceManyField>`](https://marmelab.com/react-admin/ReferenceManyField.html) or [`<ReferenceArrayField>`](https://marmelab.com/react-admin/ReferenceArrayField.html): | ||
```tsx | ||
import { Show, SimpleShowLayout, ReferenceManyField, Datagrid, TextField, DateField } from 'react-admin'; | ||
import { ListLiveUpdate } from '@react-admin/ra-realtime'; | ||
const AuthorShow = () => ( | ||
<Show> | ||
<SimpleShowLayout> | ||
<TextField source="first_name" /> | ||
<TextField source="last_name" /> | ||
<ReferenceManyField reference="books" target="author_id" label="Books"> | ||
<Datagrid> | ||
<TextField source="title" /> | ||
<DateField source="published_at" /> | ||
</Datagrid> | ||
<ListLiveUpdate /> | ||
</ReferenceManyField> | ||
</SimpleShowLayout> | ||
</Show> | ||
); | ||
``` | ||
|
||
## `onEventReceived` | ||
|
||
The `<ListLiveUpdate>` allows you to customize the side effects triggered when it receives a new event, by passing a function to the `onEventReceived` prop: | ||
|
||
```jsx | ||
import { Datagrid, List, TextField, useNotify, useRefresh } from 'react-admin'; | ||
import { ListLiveUpdate } from '@react-admin/ra-realtime'; | ||
|
||
const PostList = () => { | ||
const notify = useNotify(); | ||
const refresh = useRefresh(); | ||
|
||
const handleEventReceived = event => { | ||
const count = get(event, 'payload.ids.length', 1); | ||
notify(`${count} items updated by another user`); | ||
refresh(); | ||
}; | ||
|
||
return ( | ||
<List> | ||
<Datagrid> | ||
<TextField source="title" /> | ||
</Datagrid> | ||
<ListLiveUpdate onEventReceived={handleEventReceived} /> | ||
</List> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters