Events
The window.starkey.ethereum provider emits standard EIP-1193 events for state changes that can happen outside your dApp’s control — the user switching accounts or networks from the StarKey UI, or disconnecting the site.
These event names (accountsChanged, chainChanged, etc.) come from the EIP-1193 standard itself, not StarKey. See Supra Events for StarKey’s own event names on the Supra provider.
on(event, handler)
Subscribes to provider events.
provider.on('accountsChanged', (accounts: string[]) => setAccounts(accounts))
provider.on('chainChanged', (chainId: string) => setChainId(chainId))
provider.on('connect', (info: { chainId: string }) => console.log('connected', info.chainId))
provider.on('disconnect', (error: { code: number; message: string }) => resetWalletData())| Name | Type | Required | Description |
|---|---|---|---|
event | 'accountsChanged' | 'chainChanged' | 'connect' | 'disconnect' | yes | The event name to subscribe to. |
handler | (payload) => void | yes | Called with the event’s payload — see the table below for each event’s shape. |
Event reference
| Event | Fires when | Handler payload |
|---|---|---|
accountsChanged | Connected accounts change | string[] — updated addresses, e.g. ['0x123...'] |
chainChanged | Connected chain changes | string — hex-encoded chain ID, e.g. '0x2105' |
connect | Provider first connects | { chainId: string } |
disconnect | Provider loses connection | { code: number, message: string } |
Removing listeners
Tear down provider event handlers when your component unmounts, to avoid stale closures and duplicate handlers on re-mount:
const handleAccountsChanged = (accounts: string[]) => setAccounts(accounts)
provider.on('accountsChanged', handleAccountsChanged)
// on unmount
provider.removeListener('accountsChanged', handleAccountsChanged)Notes
accountsChangedfires with an empty array when the user disconnects all accounts from your dApp — treat that the same as adisconnect.- Refresh dependent state (accounts,
eth_chainId) inside youraccountsChanged/chainChangedhandlers — these values are scoped to the active account and chain.
Best practices
- Register all four provider events (
accountsChanged,chainChanged,connect,disconnect) together, right after you detect the provider, so your UI never gets out of sync with wallet-side changes. - Treat
disconnectas the source of truth for clearing local session state — the user can disconnect from the StarKey UI directly, not just through your dApp.
Related pages
Last updated on