- React
- React Native
- Swift
- Flutter
The useSwitchWallet hook gives you the ability to switch the user’s primary wallet.All it needs is a wallet ID as a parameter, and it resolve upon success. You can find the ID on any wallet object in Dynamic i.e. on the primaryWallet or those returned by useUserWallets:
React
Copy
Ask AI
import React from 'react';
import { useSwitchWallet, useUserWallets } from '@dynamic-labs/sdk-react-core';
const WalletSwitcher = () => {
const switchWallet = useSwitchWallet();
const userWallets = useUserWallets();
return (
<div>
{userWallets.map(wallet => (
<button
key={wallet.id}
onClick={() => switchWallet(wallet.id)}
>
{wallet.address}
</button>
))}
</div>
);
};
React Native provides the ability to switch the user’s primary wallet through the
wallets.setPrimary() method on the dynamic client.You can access the current primary wallet and switch to a different one using the reactive client:React Native
Copy
Ask AI
import { FC } from 'react';
import { dynamicClient } from '<path to client file>';
import { useReactiveClient } from '@dynamic-labs/react-hooks';
import { View, Text, TouchableOpacity } from 'react-native';
const SetFirstWalletRN: FC = () => {
const { wallets } = useReactiveClient(dynamicClient);
return (
<View>
<Text>Current primary wallet: {wallets.primary?.address}</Text>
<TouchableOpacity
onPress={() => wallets.setPrimary({ walletId: wallets.userWallets[0].id })}
>
<Text>Set first wallet as primary</Text>
</TouchableOpacity>
</View>
);
};
Swift provides the ability to switch the user’s primary wallet through the
wallets.setPrimary() method.You can access the current primary wallet and switch to a different one:Swift
Copy
Ask AI
import DynamicSwiftSDK
// Example: set the first user wallet as primary
func setFirstWalletPrimary() async {
let wallets = dynamicClient.wallets
let userWallets = wallets.userWallets
guard !userWallets.isEmpty else { return }
let first = userWallets.first!
try await wallets.setPrimary(walletId: first.id)
}
Flutter provides the ability to switch the user’s primary wallet through the
wallets.setPrimary() method.You can access the current primary wallet and switch to a different one:Flutter
Copy
Ask AI
import 'package:dynamic_sdk/dynamic_sdk.dart';
// Example: set the first user wallet as primary
Future<void> setFirstWalletPrimary() async {
final wallets = DynamicSDK.instance.wallets;
final userWallets = wallets.userWallets;
if (userWallets.isEmpty) return;
final first = userWallets.first;
await wallets.setPrimary(walletId: first.id);
}