breaking: Change the error broadcasting behavior in mutations and add throwOnError option
#2182
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Errors from mutations should be separated from direct fetches. Currently, the returned and shared
errorstate fromuseSWRwill be mutated bymutatecalls:The main use case for that is to have a global and declarative error indicator for mutations, but then it conflicts with fetching errors (e.g. resource fails to load). With the new
useSWRMutationhook, that is no longer needed because we can have a cleaner representation for both:And they will be separated and scoped to different places:
For example (1) and (2) will always share the same data and error. So if the user fails to load initially, error indicators can be shown in both places (or you can throw to trigger an upper-level error boundary). In (3) and (4) you can trigger remote mutations to update the user resource. If the mutation succeeded, (1) and (2) will be updated and rerendered with the new data. If a mutation from (3) failed, only (3) will get the error state so you can show the error indicator there, without affecting (1) (2) and (4).
throwOnErrorSince there is already
onErrorsupported inuseSWRMutation, here we are adding a new optionthrowOnErrorto mutations (bothuseSWRMutationandmutate) to define if an error should be thrown inline or not. It defaults totrue.