- React
- React Native
- JavaScript SDK
- Swift
- Flutter
- Unity
Use Tip: keep the listener component mounted at app shell level so events are captured globally; clean-up is automatic when the component unmounts.
useDynamicEvents to subscribe to wallet lifecycle events from the provider and keep UI state in sync.React
Copy
Ask AI
import { useDynamicEvents } from '@dynamic-labs/sdk-react-core';
export function WalletEventListener() {
// Fires when primary wallet changes (including account/network changes)
useDynamicEvents('primaryWalletChanged', (wallet) => {
console.log('Primary wallet changed', wallet);
});
// Fires when the primary wallet network changes
useDynamicEvents('primaryWalletNetworkChanged', (networkId) => {
console.log('Primary wallet network changed', networkId);
});
// Fires whenever user wallets list changes (add/remove/primary change/network change)
useDynamicEvents('userWalletsChanged', (params) => {
console.log('User wallets changed', params);
});
return null;
}
The client is event-first on React Native: every module exposes events you can subscribe to via
on/off (or once). Wrapping with useReactiveClient is optional and only needed if you also want reads (e.g., client.wallets.userWallets) to trigger React rerenders when they change.React Native
Copy
Ask AI
import { useEffect } from 'react';
import { useReactiveClient } from '@dynamic-labs/react-hooks';
import { dynamicClient } from '<path to client file>';
export function WalletEventListenerRN() {
const client = useReactiveClient(dynamicClient);
useEffect(() => {
const handlePrimaryChanged = (wallet) => {
console.log('Primary wallet changed', wallet);
};
client.wallets.on('primaryChanged', handlePrimaryChanged);
return () => {
client.wallets.off('primaryChanged', handlePrimaryChanged);
};
}, [client]);
return null;
}
Use
once for one-off listeners: client.wallets.once('primaryChanged', cb).Types of events
- State change events: fire when a module variable changes. Names match the variable with
Changedappended. - Custom events: fire for specific actions within the module.
How to listen
Callon(eventName, callback) on the module instance. Remove with off(eventName, callback).Copy
Ask AI
const handleAuthSuccess = (user) => {
console.log('User logged in', user);
};
dynamicClient.auth.on('authSuccess', handleAuthSuccess);
// Later, clean up
dynamicClient.auth.off('authSuccess', handleAuthSuccess);
Event catalog
Auth moduleauthInitauthSuccessauthFailedloggedOutuserProfileUpdatedauthenticatedUserChangedtokenChanged
emailVerificationFinished
smsVerificationFinished
messageSignedwalletAddedwalletRemovedprimaryChangeduserWalletsChanged
embeddedWalletCreatedhasWalletChanged
evmChangedsolanaChangedsuiChanged
errorloadedChanged
authFlowCancelledauthFlowClosedauthFlowOpened
mfaCompletionSuccess— returns{ mfaToken?: string }mfaCompletionFailure— returns{ error: unknown }
Use Notes:
onWalletProviderEvent to subscribe and clean up with the returned unsubscribe function.You can listen for:- accountsChanged
- networkChanged
- disconnected
Copy
Ask AI
import { onWalletProviderEvent } from '@dynamic-labs-sdk/client';
// Example: listen for account changes on a specific provider
const unsubscribe = onWalletProviderEvent({
walletProviderKey: 'metamaskevm',
event: 'accountsChanged',
callback: ({ addresses }) => {
console.log('Accounts changed:', addresses);
},
});
// Later, remove the listener
unsubscribe();
- Use the provider key that matches the chain/app (e.g.,
metamaskevm,metamasksol,phantomevm,phantomsol). - Re-run any logic that depends on the active account/network when these events fire (e.g., refresh balances, reconnect clients).
Coming soon.
Coming soon.
Coming soon.