Grabbing the query Key when using defaultOption's onError? #2455
-
Hi, I'll try to explain my question with an example: Let's assume I have the following query: const { ... } = useQuery('fetchTodos', api.fetchTodos) And I have configured the defaultOptions on the QueryClient like to have an const queryClient = new QueryClient({
defaultOptions: {
queries: {
onError: err => {
// handle error
},
},
},
}) Is there any way to grab the key |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What you probably want to do is use the
The callback on the queryCache solves this. It will only be called once per cache entry, and it also receives the whole see: https://react-query.tanstack.com/reference/QueryCache
|
Beta Was this translation helpful? Give feedback.
What you probably want to do is use the
onError
callback on the queryCache rather than the defaultOptions for queries. This has two reasons:onError
, to e.g. show a toast error notification, you won't track the error. There is no chaining here.onError
callback will be called for each observer, so if you have two queries with the same key, it will be called twice and thus tracked twice.The callback on the queryCache solves this. It will only be called once per cache entry, and it also receives the whole
query
as second parameter, which also has the key:see: https://react-query.tanstac…