How to set global error and success notification?
I want to set the notification for error and success of API calls globally. Is there a way to implement that? Because in almost all of my error on API call I am duplicating the code with displaying the response error message from the API. For example my API will response error in 4xx as below:
Here I want my error notification to display the message from this response whenever my status lies between 4xx.
4 Replies
Hi @dipbazz. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Yes, you can set global notifications for API calls in Refine. Refine provides
successNotification
and errorNotification
properties that you can use to customize the notifications. These properties are available in various hooks like useForm
, useUpdateMany
, useSelect
, useAutocomplete
, useList
, useInfiniteList
, and useMany
.
To display the error message from the API response, you can customize the errorNotification
property. Here's an example of how you can do it:
In this example, data.message
will display the error message from your API response. Please replace useForm
with the appropriate hook based on your requirement.
Please note that for these notifications to work, the NotificationProvider
(https://refine.dev/docs/api-reference/core/providers/notification-provider/) is required.
For more details, you can refer to the following documentation:
- useForm (https://refine.dev/docs/api-reference/core/hooks/useForm/)
- useUpdateMany (https://refine.dev/docs/api-reference/core/hooks/data/useUpdateMany/)
- useSelect (https://refine.dev/docs/api-reference/core/hooks/useSelect/)
- useAutocomplete (https://refine.dev/docs/api-reference/mui/hooks/useAutocomplete/)
- useList (https://refine.dev/docs/api-reference/core/hooks/data/useList/)
- useInfiniteList (https://refine.dev/docs/api-reference/core/hooks/data/useInfiniteList/)
- useMany (https://refine.dev/docs/api-reference/core/hooks/data/useMany/)
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below
noble-gold•12mo ago
Hello @dipbazz I think we have an issue for this one but never prioritized it. I'll check if we can do something in the coming weeks. https://github.com/refinedev/refine/issues/4006
GitHub
[FEAT] Single place to handle all notifications · Issue #4006 · ref...
Is your feature request related to a problem? Please describe. It might be useful to have a single place to configure all different notification messages for different resources, cause they might b...
deep-jade•12mo ago
Hey @batuhanw looking forward to it with the solution.