Summary

The useDynamicEvents hook can be used to subscribe to events emitted by Dynamic.

Usage

Available function and states

Auth related events

EventArgumentsDescription
authFailurereason: 'user-cancelled' | { error: unknown }Triggered whenever the auth process fails
authFlowCancellednoneTriggered when auth is attempted but fails like when user rejects connection or the network call fails
authFlowClosenoneTriggered when the modal is closed during an authentication process, regardless of failure or success
authFlowOpennoneTriggered when the modal is opened during an authentication process, regardless of failure or success
authInitmethod: stringInforms an auth attempt initialized, and provides insight into which auth option it is
logoutnoneTriggered when the user logs out

Wallet related events

EventArgumentsDescription
walletAddednewWallet: WalletTriggered whenever a wallet is added to the user profile
walletRemovedremovedWallet: WalletTriggered whenever a wallet is removed from the user profile
embeddedWalletCreatedwallet: Wallet, verifiedCredential: JwtVerifiedCredential \ undefined, user: UserProfile | undefinedTriggered when an embedded wallet is created
primaryWalletChangednewPrimaryWallet: WalletTriggered whenever the primary wallet changes
primaryWalletNetworkChangednewNetwork: number | stringTriggered whenever the primary wallet network changes
userWalletsChangedparams: UserWalletsChangedParamsTriggered whenever a wallet is added or removed from the user profile, the primary wallet changes to another one, the primary wallet network changes or a non-primary wallet network changes

OTP related events

EventArgumentsDescription
emailVerificationResultparam: boolean, email: stringTriggered when the email verification result is received
smsVerificationResultparam: boolean, phone: stringTriggered when the sms verification result is received
mfaCompletionFailureargs: { error: unknown }Emitted when there is an error verifiyng the MFA challenge
mfaCompletionSuccessargs: { mfaToken?: string }Emitted when the user succesfully completes an MFA challenge

Type Definitions

import { type UserWalletsChangedParams } from "@dynamic-labs/sdk-react-core";


type UserWalletsUpdateType =
  | {
      updateType: 'walletAdded';
      addedWallet: Wallet;
    }
  | {
      updateType: 'walletRemoved';
      removedWallet: Wallet;
    }
  | {
      updateType: 'primaryWalletChanged';
    }
  | {
      updateType: 'primaryWalletNetworkChanged';
      newNetwork: string | number;
    }
  | {
      updateType: 'nonPrimaryWalletNetworkChanged';
      newNetwork: string | number;
      affectedWallets: Wallet[];
    };

type UserWalletsChangedParams = {
  userWallets: Wallet[];
  primaryWallet: Wallet | undefined;
} & UserWalletsUpdateType;

Example

import { useDynamicEvents } from "@dynamic-labs/sdk-react-core";

const useDynamicEventsExample = () => {
  useDynamicEvents("userWalletsChanged", async (params) => {
    console.log(params);
  });

  useDynamicEvents("primaryWalletChanged", async (newPrimaryWallet) => {
    console.log(newPrimaryWallet);
  });

  useDynamicEvents("primaryWalletNetworkChanged", async (newNetwork) => {
    console.log(newNetwork);
  });

  useDynamicEvents("walletAdded", async (newWallet) => {
    console.log(newWallet);
  });
};