Appearance
useActiveWallet
The active wallet is the connection WalletsProvider currently treats as selected. Core methods still take walletId on each call — this hook is the React selection for UI.
The provider persists that walletId in localStorage, so it survives a page refresh.
- After restore, the stored wallet is selected again if it is still connected.
- If that wallet is gone (or the current one disconnects), the newest remaining connection is selected.
- A successful
connectmakes that new instance active. - Keep the selected token (and amount) in your own state.
Also available on useWalletConnections.
Example
tsx
import { useActiveWallet, useWalletConnections } from '@swapped/connect-sdk/react'
function WalletSwitcher() {
const { connections } = useWalletConnections({ sort: 'connectedAt' })
const { activeWalletId, activeWallet, setActiveWallet } = useActiveWallet()
return (
<div>
<p>{activeWallet?.name ?? 'No wallet connected'}</p>
<ul>
{connections.map(connection => (
<li key={connection.walletId}>
<button
type="button"
disabled={connection.walletId === activeWalletId}
onClick={() => setActiveWallet(connection.walletId)}
>
{connection.name}
</button>
</li>
))}
</ul>
</div>
)
}Returns
| Field | Meaning |
|---|---|
activeWalletId | Instance id, or null when none are connected |
activeWallet | WalletConnection, or null |
setActiveWallet(walletId) | Select a connected instance. No-op if that walletId is not connected |
Pass activeWalletId into useWalletBalances, transfer plan, and other calls that take a walletId.