Connecting & Disconnecting
Manage wallet connection state and multi-wallet selection.
Recipe
"use client";
import { useWallet } from "@solana/wallet-adapter-react";
const { connect, disconnect, connected, connecting, wallet, select } = useWallet();When to reach for this:
- Connect buttons, account chips, and session reset.
- Switching wallets without full page reload.
- Clearing state on logout.
Working Example
"use client";
import { useCallback } from "react";
import { useWallet } from "@solana/wallet-adapter-react";
import { useWalletModal } from "@solana/wallet-adapter-react-ui";
export function WalletControls() {
const { connected, disconnect, wallet } = useWallet();
const { setVisible } = useWalletModal();
const handleConnect = useCallback(() => setVisible(true), [setVisible]);
if (!connected) {
return <button onClick={handleConnect}>Select wallet</button>;
}
return (
<div>
<span>{wallet?.adapter.name}</span>
<button onClick={() => disconnect()}>Disconnect</button>
</div>
);
}What this demonstrates:
- Modal handles multi-wallet selection.
disconnectclears adapter session.- Show adapter name for support debugging.
Deep Dive
How It Works
- Connect negotiates with selected adapter via Wallet Standard.
- State propagates through React context.
- Disconnect should clear app caches keyed by pubkey.
- Reconnect may reuse prior adapter when autoConnect enabled.
Gotchas
- Stale pubkey in global state after disconnect - wrong user data. Fix: reset stores on disconnect.
- connect() without modal when multiple wallets - ambiguous adapter. Fix: always show picker.
- Ignoring connecting flag - double-click errors. Fix: disable button while connecting.
- Mobile deep link not returning - stuck connecting. Fix: timeout + retry copy.
- Assuming connect = sign permission - each tx still needs approval.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| Embedded wallet (Phantom embedded) | Consumer onboarding | Full self-custody positioning |
| SIWS + server session | Web2-style login | Fully on-chain auth only |
FAQs
Devnet or mainnet?
Match RPC URL, wallet network, and program IDs to one cluster.
kit vs web3.js?
Use @solana/kit 7.0.0 for new frontend code.
Testing?
Surfpool 0.12.0 or devnet with faucet SOL.
Commitment level?
confirmed for UX; finalized for high-value reads.
Next.js App Router?
Providers in client layout; reads can be server-side.
Wallet rejected sign?
Show retry; never assume approval.
Priority fees?
Compute budget instructions on congested mainnet.
Mobile?
Use Mobile Wallet Adapter for React Native apps.
Security?
Never expose server keypairs to the browser.
IDL changes?
Regenerate typed clients in CI.
Related
- Autoconnect & Session UX - persistent sessions
- Wallet Adapter - provider setup
- Signing Transactions & Messages - after connect
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.