DocsReact HooksuseFollowButton

useFollowButton

The useFollowButton hook manages the state and actions for a follow button component. It determines the current follow state between a lookupAddress and a connectedAddress, and provides functions to handle follow, unfollow, block, and mute actions.

Features

  • Input Validation: Validates both addresses using viem’s isAddress function
  • Error Handling: Comprehensive error state management with error display and recovery
  • Accessibility: Built-in ARIA attributes for screen reader support
  • Async Operations: Proper async/await handling with try/catch error management
  • TypeScript Support: Exported interfaces for better type safety

Add to your project

import { useFollowButton } from 'ethereum-identity-kit'
 
export default function FollowButtonComponent() {
  const { 
    buttonText, 
    buttonState, 
    handleAction, 
    isLoading, 
    isDisabled,
    error,
    clearError,
    ariaLabel,
    ariaPressed,
    disableHover, 
    setDisableHover 
  } = useFollowButton({
    lookupAddress: '0x1234...abcd',
    connectedAddress: '0xabcd...1234',
  })
 
  // Create your own loading states
  if (isLoading) return <div>Loading...</div>
 
  return (
    <button
      className={`follow-button ${disableHover ? 'no-hover' : ''} ${error ? 'error' : ''}`}
      onClick={handleAction}
      onMouseEnter={() => setDisableHover(false)}
      disabled={isDisabled}
      aria-label={ariaLabel}
      aria-pressed={ariaPressed}
      title={error || undefined}
    >
      {buttonText}
    </button>
  )
}

Parameters

ParameterDescriptionRequiredDefault Value
lookupAddressEthereum address to manage the follow state for.Yes-
connectedAddressEthereum address of the currently connected user.No-
selectedListList number to manage the follow state for; defaults to the primary list.No-

The hook now automatically validates addresses using viem’s isAddress function. Invalid addresses will set an error state instead of throwing.

Return Values

Return ValueDescription
buttonTextThe text to display on the follow button, indicating the current or pending follow state.
buttonStateThe current state of the button, such as ‘Follow’, ‘Following’, ‘Blocked’, etc.
handleActionAsync function to handle button clicks, now with proper error handling.
isLoadingBoolean indicating if the follow state is currently loading.
isDisabledBoolean combining multiple disable conditions (loading, invalid addresses, etc.).
errorError message string if an operation fails, null otherwise.
clearErrorFunction to manually clear the current error state.
ariaLabelComputed ARIA label for screen reader accessibility.
ariaPressedARIA pressed state for toggle button semantics.
pendingStateThe pending state of the follow action, if any (e.g., ‘Pending Following’, ‘Pending Block’).
disableHoverBoolean indicating if hover effects should be disabled.
setDisableHoverFunction to set the disableHover state.

TypeScript Interface

interface UseFollowButtonParams {
  lookupAddress: string
  connectedAddress?: string
  selectedList?: number
}
 
interface UseFollowButtonReturn {
  buttonText: string
  buttonState: string
  handleAction: () => Promise<void>
  isLoading: boolean
  isDisabled: boolean
  error: string | null
  clearError: () => void
  ariaLabel: string
  ariaPressed: boolean | undefined
  pendingState: string | null
  disableHover: boolean
  setDisableHover: (value: boolean) => void
}

Error Handling

The hook now includes comprehensive error handling:

const { error, clearError, handleAction } = useFollowButton({
  lookupAddress: '0x1234...abcd',
})
 
// Errors are automatically displayed and can be manually cleared
if (error) {
  console.error('Follow button error:', error)
  // Optionally clear the error after a delay
  setTimeout(clearError, 5000)
}

Accessibility Features

The hook provides built-in accessibility attributes:

<button
  aria-label={ariaLabel} // e.g., "Follow vitalik.eth"
  aria-pressed={ariaPressed} // true when following
  // ... other props
>

Notes

  • The handleAction function is now async and includes try/catch error handling
  • Address validation happens automatically using viem’s isAddress
  • Errors are automatically cleared when the user retries an action
  • The isDisabled property combines loading state, address validation, and other conditions
  • The hook uses an ACTION_CONFIG pattern internally for cleaner action organization