Skip to main content

resetError

Description: A utility function returned by the useFetch hook used to manually clear an error state from the cache and the hook's local state.

  • How To: Destructure resetError from the useFetch return object. Call it to dismiss an active error state, which is useful for "Try Again" UI patterns or manual error handling.
    // Correct: Using resetError to clear a failed state
    const { error, resetError, refetch } = useFetch({
    key: 'user-profile',
    data: () => actionGetUser()
    });

    if (error) {
    return (
    <ErrorBanner
    message="Failed to load profile"
    onDismiss={() => resetError()} // Clears the error state in cache
    onRetry={() => refetch()} // Manually triggers a new fetch
    />
    );
    }
  • Best Practice: Use resetError when you want to return the UI to a clean (non-error) state without necessarily forcing an immediate refetch. It allows for a better user experience by letting the user decide when to retry.

Why

  • Provides granular control over the error lifecycle.
  • Enables "Dismiss" functionality for error messages.
  • Prevents stale error states from blocking subsequent UI interactions.