global and local onError callbacks related questions #3125
-
I have few questions, could you pl. answer them: a. Is it possible to execute the onError both at a global and local level for a given query or mutation. For example, use global onError to display toast and local onError to handle local logic, for example navigate to this particular route if error occurs. b. Is it possible to execute global onError conditionally, like I do not want to show a toast for some errors. I think it might be achieved by adding a boolean key/value in the 'meta' object and check if it is set to true, then only display toast. Right? c. Here is an example that displays a toast locally:
Now react-query also lets us display toasts at a global level indicating that some validation failed. Keeping this in view, I am looking for code something like this:
As of now, I don't know react-query much. So pl. confirm if the if/else logic is correct. Is this pattern okay with you, or would you advise local onError handler for each query and mutation placed immediately after the server response in the local functions themselves? d. In order to display toast for failed background queries, we are writing a global onError callback for "QueryCache". For the same reason, it needs to be written for MutationCache as well. Am I right? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
if by global you man the
yes, exactly. If the
I think that's totally fine. You can hoist up whatever logic you think would be repetitive on query levels to the global level.
Yes, if you want error toasts for mutations as well, they have the same global callbacks on the |
Beta Was this translation helpful? Give feedback.
if by global you man the
onError
callback on thequeryCache
then yes, because the queryCache callbacks always get executed. However, theonError
specified underdefaultOptions
of thequeryClient
will be overwritten by specific handlers onuseQuery
because thedefaultOptions
are "fallbacks" / "defaults".