Skip to content

Commit 2037cb5

Browse files
authored
Merge pull request marmelab#10372 from marmelab/doc-useUpdate-returnPromise
[Doc] Update `useUpdate` doc to explain `returnPromise` option
2 parents 29f4cab + 9bdee82 commit 2037cb5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/useUpdate.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const IncreaseLikeButton = () => {
110110
- `onError`,
111111
- `onSettled`,
112112
- `onSuccess`,
113+
- `returnPromise`.
113114

114115
```jsx
115116
const notify = useNotify();
@@ -326,6 +327,32 @@ In `optimistic` mutation mode, `onSuccess` executes *before* the `dataProvider.u
326327

327328
In `undoable` mutation mode, `onSuccess` executes *before* the `dataProvider.update()` is called. The actual call to the dataProvider is delayed until the update notification hides. If the user clicks the undo button, the `dataProvider.update()` call is never made. The callback receives no argument.
328329

330+
## `returnPromise`
331+
332+
By default, the `update` callback that `useUpdate` returns is synchronous and returns nothing. To execute a side effect after the mutation has succeeded, you can use the `onSuccess` callback.
333+
334+
If this is not enough, you can use the `returnPromise` option so that the `update` callback returns a promise that resolves when the mutation has succeeded and rejects when the mutation has failed.
335+
336+
This can be useful if the server changes the record, and you need the updated data to update another record.
337+
338+
```jsx
339+
const [update] = useUpdate(
340+
'posts',
341+
{ id: record.id, data: { isPublished: true } },
342+
{ returnPromise: true }
343+
);
344+
const [create] = useCreate('auditLogs');
345+
346+
const publishPost = async () => {
347+
try {
348+
const post = await update();
349+
create('auditLogs', { data: { action: 'publish', recordId: post.id, date: post.updatedAt } });
350+
} catch (error) {
351+
// handle error
352+
}
353+
};
354+
```
355+
329356
## TypeScript
330357

331358
The `useUpdate` hook accepts a generic parameter for the record type and another for the error type:

0 commit comments

Comments
 (0)