Skip to content

Commit a3b1da8

Browse files
committed
Document how to handle errors for resumed mutations
1 parent b8b13c5 commit a3b1da8

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

docs/DataProviders.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,4 +1017,41 @@ queryClient.setMutationDefaults('banUser', {
10171017
return dataProvider.banUser(userId);
10181018
},
10191019
});
1020-
```
1020+
```
1021+
1022+
## Handling Errors For Resumed Mutations
1023+
1024+
If you enabled offline support, users might trigger mutations while being actually offline. When they're back online, react-query will _resume_ those mutations and they might fail for other reasons (server side validation or errors). However, as users might have navigated away from the page that triggered the mutation, they won't see any notification.
1025+
1026+
To handle this scenario, you must register default `onError` side effects for all mutations (react-admin default ones or custom). If you want to leverage react-admin notifications, you can use a custom layout:
1027+
1028+
```tsx
1029+
// in src/Layout.tsx
1030+
export const MyLayout = ({ children }: { children: React.ReactNode }) => {
1031+
const queryClient = useQueryClient();
1032+
const notify = useNotify();
1033+
1034+
React.useEffect(() => {
1035+
const mutationKeyFilter = []; // An empty array targets all mutations
1036+
queryClient.setMutationDefaults([], {
1037+
onSettled(data, error) {
1038+
if (error) {
1039+
notify(error.message, { type: 'error' });
1040+
}
1041+
},
1042+
});
1043+
}, [queryClient, notify]);
1044+
1045+
return (
1046+
<Layout>
1047+
{children}
1048+
</Layout>
1049+
);
1050+
}
1051+
```
1052+
1053+
Note that this simple example will only show the error message as it was received. Users may not have the context to understand the error (what record or operation it relates to).
1054+
Here are some ideas for a better user experience:
1055+
1056+
- make sure your messages allow users to go to the pages related to the errors (you can leverage [custom notifications](./useNotify.md#custom-notification-content) for that)
1057+
- store the notifications somewhere (server side or not) and show them in a custom page with proper links, etc.

0 commit comments

Comments
 (0)