Events
Cosmostation exposes wallet state changes so your dApp can stay synchronized
with the user’s wallet state.
This section focuses only on account change events.
Using Aptos Wallet Adapter
When using @aptos-labs/wallet-adapter-react, you do not need to manually
subscribe to account change events.
The useWallet() hook internally listens to wallet events and always provides
the latest account state.
accountupdates automatically when the user switches accounts in the wallet- React components re-render when the account changes
- No explicit event registration or cleanup is required
Example
import { useWallet } from '@aptos-labs/wallet-adapter-react';
const { account, connected } = useWallet();
const getAccountState = () => {
try {
if (!connected) throw new Error('No connected wallet');
console.log('Current account:', account);
} catch (error) {
console.error(error);
}
};Behavior
- Account changes are propagated automatically
- The returned
accountobject reflects the currently selected account - This is the recommended approach for React-based dApps
Account Change (Vanilla Code)
If you are not using the wallet adapter, you can subscribe directly to account change events from the injected provider.
Example
const handler = (account) => {
console.log('Account changed:', account);
};
try {
const provider = aptos();
provider.onAccountChange(handler);
} catch (e) {
console.error(e);
}Specification Reference
The vanilla onAccountChange API follows the Aptos Wallet Standard
AptosOnAccountChangeMethod.
For the exact method signature and event behavior, refer to the official source: