resetError
Description: Manually clear the error state from the global cache and local state.
- How To: Call the
resetErrorfunction returned by theuseMutationhook. This will clear any current error state associated with the mutation in themutateCacheand update the hook's local state.const { mutate, error, resetError } = useMutation({mutationKey: 'update-settings'});if (error) {return (<div className="alert-error"><p>Failed: {error.message}</p><button onClick={resetError}>Dismiss</button></div>);} - Best Practice: Use
resetErrorto allow users to manually dismiss or retry after a failed mutation. This is essential for preventing the user from being stuck in an error state and provides a clear path to recovery.
Deep Dive: How resetError works step by step
Example:
function ActionButton() {
const { mutate, error, resetError } = useMutation({
mutationKey: 'global-action'
});
return (
<div>
{error && <ErrorMessage message={error.message} onDismiss={resetError} />}
<button onClick={() => mutate(action)}>Execute</button>
</div>
);
}
What happens step by step when resetError is called:
- User clicks "Dismiss" →
resetError()is called. useMutationcallsmutation.resetError()→ Interacts with the globalDataClient.- Global cache entry is updated → The entry for
'global-action'inmutateCachehas itserrorset tonullandstatusset to'stale'. - Subscription system is notified → The
DataClientpublishes an update for the key'global-action'. - Component is notified → The
useSyncExternalStoreinsideuseMutationreceives the update. - Hook re-renders → The local
errorvariable becomesnull, andisErrorbecomesfalse. - UI is updated → The
<ErrorMessage />component is unmounted, and the user can see the original state.
Key insight: resetError is a global command. If multiple components are observing the same mutationKey, calling resetError in one will clear the error and trigger a re-render in all of them. This ensures a consistent error-dismissal experience across the entire application.