You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Create.md
+29Lines changed: 29 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -60,6 +60,7 @@ You can customize the `<Create>` component using the following props:
60
60
*`className`: passed to the root component
61
61
*[`component`](#component): override the root component
62
62
*[`disableAuthentication`](#disableauthentication): disable the authentication check
63
+
*[`mutationMode`](#mutationmode): switch to optimistic or undoable mutations (pessimistic by default)
63
64
*[`mutationOptions`](#mutationoptions): options for the `dataProvider.create()` call
64
65
*[`record`](#record): initialize the form with a record
65
66
*[`redirect`](#redirect): change the redirect location after successful creation
@@ -154,6 +155,34 @@ const PostCreate = () => (
154
155
);
155
156
```
156
157
158
+
## `mutationMode`
159
+
160
+
The `<Create>` view exposes a Save button, which perform a "mutation" (i.e. it creates the data). React-admin offers three modes for mutations. The mode determines when the side effects (redirection, notifications, etc.) are executed:
161
+
162
+
-`pessimistic` (default): The mutation is passed to the dataProvider first. When the dataProvider returns successfully, the mutation is applied locally, and the side effects are executed.
163
+
-`optimistic`: The mutation is applied locally and the side effects are executed immediately. Then the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
164
+
-`undoable`: The mutation is applied locally and the side effects are executed immediately. Then a notification is shown with an undo button. If the user clicks on undo, the mutation is never sent to the dataProvider, and the page is refreshed. Otherwise, after a 5 seconds delay, the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
165
+
166
+
By default, pages using `<Create>` use the `pessimistic` mutation mode as the new record identifier is often generated on the backend. However, should you decide to generate this identifier client side, you can change the `mutationMode` to either `optimistic` or `undoable`:
You can customize the options you pass to react-query's `useMutation` hook, e.g. to pass [a custom `meta`](./Actions.md#meta-parameter) to the `dataProvider.create()` call.
`meta` is helpful for passing additional information to the dataProvider. For instance, you can pass the current user to let a server-side audit system know who made the creation.
86
+
87
+
## Options
88
+
89
+
`useCreate`'s third parameter is an `options` object with the following properties:
Additional options are passed to [React Query](https://tanstack.com/query/v5/)'s [`useMutation`](https://tanstack.com/query/v5/docs/react/reference/useMutation) hook. This includes:
118
+
119
+
-`gcTime`,
120
+
-`networkMode`,
121
+
-`onMutate`,
122
+
-`retry`,
123
+
-`retryDelay`,
124
+
-`mutationKey`,
125
+
-`throwOnError`.
126
+
127
+
Check [the useMutation documentation](https://tanstack.com/query/v5/docs/react/reference/useMutation) for a detailed description of all options.
128
+
129
+
**Tip**: In react-admin components that use `useCreate`, you can override the mutation options using the `mutationOptions` prop. This is very common when using mutation hooks like `useCreate`, e.g., to display a notification or redirect to another page.
130
+
131
+
For instance, here is a button using `<Create mutationOptions>` to notify the user of success using the bottom notification banner:
The `useCreate` hook returns an array with two values:
161
+
162
+
- the `create` callback, and
163
+
- a mutation state object with the following properties:
164
+
-`data`,
165
+
-`error`,
166
+
-`isError`,
167
+
-`isIdle`,
168
+
-`isPending`,
169
+
-`isPaused`,
170
+
-`isSuccess`,
171
+
-`failureCount`,
172
+
-`failureReason`,
173
+
-`mutate`,
174
+
-`mutateAsync`,
175
+
-`reset`,
176
+
-`status`,
177
+
-`submittedAt`,
178
+
-`variables`.
179
+
180
+
The `create` callback can be called with a `resource` and a `param` argument, or, if these arguments were defined when calling `useCreate`, with no argument at all:
181
+
182
+
```jsx
183
+
// Option 1: define the resource and params when calling the callback
184
+
const [create, { isPending }] =useCreate();
185
+
consthandleClick= () => {
186
+
create(resource, params, options);
187
+
};
188
+
189
+
// Option 2: define the resource and params when calling the hook
For a detailed description of the mutation state, check React-query's [`useMutation` documentation](https://tanstack.com/query/v5/docs/react/reference/useMutation).
197
+
198
+
Since `useCreate` is mainly used in event handlers, success and error side effects are usually handled in the `onSuccess` and `onError` callbacks. In most cases, the mutation state is just used to disable the save button while the mutation is pending.
199
+
200
+
## `mutationMode`
201
+
202
+
The `mutationMode` option lets you switch between three rendering modes, which change how the success side effects are triggered:
203
+
204
+
-`pessimistic` (the default)
205
+
-`optimistic`, and
206
+
-`undoable`
207
+
208
+
**Note**: For `optimistic` and `undoable` modes, the record `id` must be generated client side. Those two modes are useful when building local first applications.
209
+
210
+
Here is an example of using the `optimistic` mode:
211
+
212
+
```jsx
213
+
// In optimistic mode, ids must be generated client side
In `pessimistic` mode, the `onSuccess` side effect executes *after* the dataProvider responds.
227
+
228
+
In `optimistic` mode, the `onSuccess` side effect executes just before the `dataProvider.create()` is called, without waiting for the response.
229
+
230
+
In `undoable` mode, the `onSuccess` side effect fires immediately. The actual call to the dataProvider is delayed until the create notification hides. If the user clicks the undo button, the `dataProvider.create()` call is never made.
231
+
232
+
See [Optimistic Rendering and Undo](./Actions.md#optimistic-rendering-and-undo) for more details.
233
+
234
+
**Tip**: If you need a side effect to be triggered after the dataProvider response in `optimistic` and `undoable` modes, use the `onSettled` callback.
235
+
236
+
## `onError`
237
+
238
+
The `onError` callback is called when the mutation fails. It's the perfect place to display an error message to the user.
**Tip**: The `onSettled` callback is perfect for calling a success side effect after the dataProvider response in `optimistic` and `undoable` modes.
273
+
274
+
## `onSuccess`
275
+
276
+
The `onSuccess` callback is called when the mutation succeeds. It's the perfect place to display a notification or to redirect the user to another page.
In `pessimistic` mutation mode, `onSuccess` executes *after* the `dataProvider.create()` responds. React-admin passes the result of the `dataProvider.create()` call as the first argument to the `onSuccess` callback.
294
+
295
+
In `optimistic` mutation mode, `onSuccess` executes *before* the `dataProvider.create()` is called, without waiting for the response. The callback receives no argument.
296
+
297
+
In `undoable` mutation mode, `onSuccess` executes *before* the `dataProvider.create()` is called. The actual call to the dataProvider is delayed until the create notification hides. If the user clicks the undo button, the `dataProvider.create()` call is never made. The callback receives no argument.
298
+
299
+
## `returnPromise`
300
+
301
+
By default, the `create` callback that `useCreate` returns is synchronous and returns nothing. To execute a side effect after the mutation has succeeded, you can use the `onSuccess` callback.
302
+
303
+
If this is not enough, you can use the `returnPromise` option so that the `create` callback returns a promise that resolves when the mutation has succeeded and rejects when the mutation has failed.
304
+
305
+
This can be useful if the server changes the record, and you need the newly created data to create/update another record.
0 commit comments