You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SWR offers fantastic abstractions when it comes to data fetching. However, there seem to have been a deliberated design decision to exclude some of the abstractions when it comes to mutations.
Rationale
We would like to use the same API/abstractions for both queries and mutations.
Use case
Consider a user profile page, that has a checkbox:
[ ] Email me marketing updates
The value of this checkbox is already included in the payload of the page (so no need to fetch it at any point). In other words, there is some initial data regarding this value.
When the user clicks the checkbox, we want to send a mutation to the backend.
If the user is offline (or the target endpoint is down), we would like to show a Toast (global onError configuration) and retry.
With the current API, we can use trigger alright, but would need to do the retries ourselves...
Issues
mutate does not handle errors if the remote mutation fails
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
SWR offers fantastic abstractions when it comes to data fetching. However, there seem to have been a deliberated design decision to exclude some of the abstractions when it comes to mutations.
Rationale
We would like to use the same API/abstractions for both queries and mutations.
Use case
Consider a user profile page, that has a checkbox:
onError
configuration) and retry.With the current API, we can use
trigger
alright, but would need to do the retries ourselves...Issues
mutate does not handle errors if the remote mutation fails
I guess this is because
onError
is already used for throws from the fetcher?trigger does
This does work with
useSWRMutation
:Neither mutate nor trigger support retries
Title says it all.
mutate supports
fallbackData
but trigger doesn'tYou could overcome this with
React.useState
, but it is tempting to usedata
as a state variable.You can provide
fallbackData
withmutate
, but not withtrigger
- you have to wait for the mutation to fire beforedata
is populated.Current support
Beta Was this translation helpful? Give feedback.
All reactions