Skip to main content

Events

EIP-1193 providers emit events for account changes, chain changes, and connection status using the standard EventEmitter pattern.

Overview

Providers emit four standard events:

Available Events

accountsChanged

Emitted when the active accounts change (e.g., user switches accounts in wallet). Event Data: string[] - Array of addresses
Use cases:
  • Update UI when user switches wallets
  • Re-fetch user-specific data
  • Prompt user to reconnect

chainChanged

Emitted when the chain changes (e.g., user switches from mainnet to testnet). Event Data: string - Chain ID (hex-encoded)
Use cases:
  • Reload application to avoid stale state
  • Update network-specific configurations
  • Display appropriate chain indicator
When chain changes, app state may be invalid. Most apps should reload: window.location.reload().

connect

Emitted when provider becomes connected to a chain. Event Data: {chainId: string} - Connection info
Use cases:
  • Initialize app after connection established
  • Fetch initial blockchain data
  • Enable blockchain-dependent features

disconnect

Emitted when provider disconnects from all chains. Event Data: {code: number, message: string} - Error info
Use cases:
  • Clean up subscriptions and listeners
  • Show reconnection UI
  • Save user state before disconnect

Event Management

Adding Listeners

Use on() or addEventListener():

Removing Listeners

Use removeListener() or removeEventListener():
Always use the same function reference when removing listeners. Arrow functions created inline can’t be removed.

One-Time Listeners

Use once() for single-use handlers:

Usage Patterns

React Hook

Chain Change Handler

Connection Monitor

Account Switcher

Best Practices

  1. Always remove listeners when component unmounts or is no longer needed
  2. Use same function reference for add/remove to work correctly
  3. Reload on chainChanged to avoid stale state issues
  4. Handle empty accounts array - user may disconnect wallet
  5. Test disconnection scenarios - network issues, manual disconnects

Error Handling

Events themselves don’t throw errors, but you can wrap handlers:

Key Differences from Custom Event Systems

For blockchain data subscriptions (blocks, logs, transactions), use JSON-RPC subscription methods like eth_subscribe.