Skip to main content

useCurrentWallet

The useCurrentWallet hook retrieves the wallet that is currently connected to the dApp, if one exists.

Live Editor
function withProviders(
    Component: React.FunctionComponent<object>,
    walletProviderProps?: Omit<ComponentProps<typeof WalletProvider>, 'children'>,
) {
    // Work around server-side pre-rendering
    const queryClient = new QueryClient();
    const networks = {
        testnet: { url: getFullnodeUrl('testnet') },
    };

    return () => {
        const [shouldRender, setShouldRender] = useState(false);
        useEffect(() => {
            setShouldRender(true);
        }, [setShouldRender]);

        if (!shouldRender) {
            return null;
        }

        return (
            <QueryClientProvider client={queryClient}>
                <IotaClientProvider networks={networks}>
                    <WalletProvider {...walletProviderProps}>
                        <Component />
                    </WalletProvider>
                </IotaClientProvider>
            </QueryClientProvider>
        );
    };
}

const UseCurrentWalletExample = withProviders(() => {
    const { currentWallet, connectionStatus } = useCurrentWallet();

    return (
        <div style={{ padding: 20 }}>
            <ConnectButton />
            {connectionStatus === 'connected' ? (
                <div>
                    <h2>Current wallet:</h2>
                    <div>Name: {currentWallet.name}</div>
                    <div>
                        Accounts:
                        <ul>
                            {currentWallet.accounts.map((account) => (
                                <li key={account.address}>- {account.address}</li>
                            ))}
                        </ul>
                    </div>
                </div>
            ) : (
                <div>Connection status: {connectionStatus}</div>
            )}
        </div>
    );
});

render(<UseCurrentWalletExample/>)
Result
Loading...

Wallet properties

  • name - The name of the wallet.
  • version - The version of the wallet as a string.
  • icon - A data URL of the wallet icon as an SVG.
  • accounts - An array of accounts that are available in the wallet.
  • features - An object with all the wallet-standard features implemented by the wallet.
  • chains - An array of chain identifiers that the wallet supports.

Connection status properties

  • connectionStatus

    • disconnected - When no wallet is connected to the dApp.
    • connecting - When a wallet connection attempt is in progress.
    • connected - When a wallet is connected to the dApp.
  • isDisconnected - A derived boolean from the status variable above, provided for convenience.

  • isConnecting - A derived boolean from the status variable above, provided for convenience.

  • isConnected - A derived boolean from the status variable above, provided for convenience.