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
- 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)
- Reload application to avoid stale state
- Update network-specific configurations
- Display appropriate chain indicator
connect
Emitted when provider becomes connected to a chain. Event Data:{chainId: string} - Connection info
- 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
- Clean up subscriptions and listeners
- Show reconnection UI
- Save user state before disconnect
Event Management
Adding Listeners
Useon() or addEventListener():
Removing Listeners
UseremoveListener() or removeEventListener():
One-Time Listeners
Useonce() for single-use handlers:
Usage Patterns
React Hook
Chain Change Handler
Connection Monitor
Account Switcher
Best Practices
- Always remove listeners when component unmounts or is no longer needed
- Use same function reference for add/remove to work correctly
- Reload on chainChanged to avoid stale state issues
- Handle empty accounts array - user may disconnect wallet
- 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.
Related
- Getting Started - Installation and setup
- Method API - Making JSON-RPC requests
- Error Handling - Handling errors

