Skip to content

Commit a3db27c

Browse files
authored
Merge pull request #10858 from marmelab/offline-support-edit-view
Add offline support to `<EditBase>` and `<Edit>`
2 parents 585432c + f16ec09 commit a3db27c

File tree

9 files changed

+700
-40
lines changed

9 files changed

+700
-40
lines changed

docs/Edit.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ You can customize the `<Edit>` component using the following props:
7676
| `id` | Optional | `string`/`number` | - | The id of the record to edit |
7777
| `mutationMode` | Optional | `'undoable' \| 'optimistic' \| 'pessimistic'` | `'undoable'` | Switch to optimistic or pessimistic mutations |
7878
| `mutationOptions` | Optional | `object` | - | Options for the `dataProvider.update()` call |
79+
| `offline` | Optional | `ReactNode` | | The component to render when there is no connectivity and the record isn't in the cache
7980
| `queryOptions` | Optional | `object` | - | Options for the `dataProvider.getOne()` call |
8081
| `redirect` | Optional | `'list' \| 'show' \| false \| function` | `'list'` | Change the redirect location after successful update |
8182
| `resource` | Optional | `string` | - | Override the name of the resource to edit |
@@ -539,6 +540,38 @@ The default `onError` function is:
539540

540541
**Tip**: If you want to have different failure side effects based on the button clicked by the user, you can set the `mutationOptions` prop on the `<SaveButton>` component, too.
541542

543+
## `offline`
544+
545+
By default, `<EditBase>` renders nothing when there is no connectivity and the record hasn't been cached yet. You can provide your own component via the `offline` prop:
546+
547+
```jsx
548+
import { Edit } from 'react-admin';
549+
550+
export const PostEdit = () => (
551+
<Edit offline={<p>No network. Could not load the post.</p>}>
552+
...
553+
</Edit>
554+
);
555+
```
556+
557+
**Tip**: If the record is in the Tanstack Query cache but you want to warn the user that they may see an outdated version, you can use the `<IsOffline>` component:
558+
559+
```jsx
560+
import { Edit, IsOffline } from 'react-admin';
561+
import { Alert } from '@mui/material';
562+
563+
export const PostEdit = () => (
564+
<Edit offline={<p>No network. Could not load the post.</p>}>
565+
<IsOffline>
566+
<Alert severity="warning">
567+
You are offline, the data may be outdated
568+
</Alert>
569+
</IsOffline>
570+
...
571+
</Edit>
572+
);
573+
```
574+
542575
## `queryOptions`
543576

544577
`<Edit>` calls `dataProvider.getOne()` on mount via react-query's `useQuery` hook. You can customize the options you pass to this hook by setting the `queryOptions` prop.
@@ -550,7 +583,7 @@ This can be useful e.g. to pass [a custom `meta`](./Actions.md#meta-parameter) t
550583
```jsx
551584
import { Edit, SimpleForm } from 'react-admin';
552585

553-
export const PostShow = () => (
586+
export const PostEdit = () => (
554587
<Edit queryOptions={{ meta: { foo: 'bar' } }}>
555588
<SimpleForm>
556589
...

0 commit comments

Comments
 (0)