useWallet

The useWallet adapter provides reactive access to wallet state and methods from the WalletManager. It's available for React, Vue, SolidJS, and Svelte, offering a consistent API across frameworks while respecting each framework's patterns and conventions.

import { useNetwork, useWallet } from '@txnlab/use-wallet-react'

function WalletStatus() {
  const { activeAccount } = useWallet()
  const { activeNetwork } = useNetwork()

  if (!activeAccount) {
    return <div>No wallet connected</div>
  }

  return (
    <div>
      <div>Account: {activeAccount.name}</div>
      <div>Network: {activeNetwork}</div>
    </div>
  )
}

Core Functionality

All framework adapters provide access to the same core wallet state and methods, with framework-specific reactive wrappers.

State Properties

Methods

Framework-Specific Usage

Each framework adapter provides the same functionality with patterns optimized for that framework's ecosystem.

Setup

See the framework-specific guidesarrow-up-right for detailed setup instructions.

State Access

React provides state directly as properties from the hook:

Complete Example

Here's a complete example showing wallet connection, transaction signing, and error handling in each framework:

Framework-Specific Considerations

React

  • Uses React's Context API for state management

  • Compatible with React 16.8+ (requires Hooks)

Vue

  • Integrated with Vue's reactivity system using ref and computed

  • Can be used in Options API via inject

  • State properties are automatically unwrapped in templates

  • Compatible with Vue 3.x

SolidJS

  • Uses Solid's fine-grained reactivity system

  • State values are accessed via getter functions for reactivity

  • Compatible with Solid 1.x

Svelte

  • Uses Svelte's Context API for state management

  • Reactive values are accessed via .current property (see Svelte Guide)

  • Compatible with Svelte 5.x

Error Handling

By design, wallet operations propagate errors to the consuming application. This allows applications to handle user feedback in a way that best fits their UI/UX patterns, such as displaying toasts, modals, or in-line error messages.

Always wrap async operations in try/catch blocks:

See the Connect Wallet Menu and Signing Transactions guide for more detailed error handling examples.

TypeScript Support

All framework adapters are written in TypeScript and provide full type definitions. State and method types are consistent across frameworks, with framework-specific wrappers where needed (e.g., Vue refs, Solid signals).

See Also

Last updated