Skip to content

Commit

Permalink
[Doc] Update useUpdate doc to explain returnPromise option
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Nov 21, 2024
1 parent b03a514 commit 9bdee82
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/useUpdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const IncreaseLikeButton = () => {
- `onError`,
- `onSettled`,
- `onSuccess`,
- `returnPromise`.

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

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.

## `returnPromise`

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.

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.

This can be useful if the server changes the record, and you need the updated data to update another record.

```jsx
const [update] = useUpdate(
'posts',
{ id: record.id, data: { isPublished: true } },
{ returnPromise: true }
);
const [create] = useCreate('auditLogs');

const publishPost = async () => {
try {
const post = await update();
create('auditLogs', { data: { action: 'publish', recordId: post.id, date: post.updatedAt } });
} catch (error) {
// handle error
}
};
```

## TypeScript

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

0 comments on commit 9bdee82

Please sign in to comment.