Svelte

The Svelte adapter (@txnlab/use-wallet-svelte) provides primitives for integrating use-wallet into Svelte applications. This guide covers how to set up the adapter and use its features effectively.

Setup

After installing the package and any required wallet dependencies (see Installation) and configuring your WalletManager, call useWalletContext in your base +layout.svelte:

import {
  useWalletContext,
  WalletManager,
  NetworkId
} from '@txnlab/use-wallet-svelte'

// Create manager instance (see Configuration guide)
const manager = new WalletManager({
  wallets: [...],
  networks: {...},
  defaultNetwork: NetworkId.TESTNET
})

useWalletContext(manager)

This makes the wallet functionality available throughout your application via Svelte's primitives.

Using the Primitives

The Svelte adapter provides two primitives for accessing wallet functionality. In v4.0.0, network-related features were moved from useWallet into a new useNetwork primitive to provide better separation of concerns:

useWallet

The useWallet primitive provides access to wallet management features. Here's an example showing some commonly used values:

Note that unlike React's hooks, Svelte's primitives return functions that need to be called to access their current value. The values are automatically tracked and will trigger reactivity when they change.

For a complete list of all available properties and methods, see the useWallet API Reference.

useNetwork

The useNetwork primitive serves two primary functions: managing the active network and supporting runtime node configuration.

Active network management enables users to switch between different networks.

Runtime node configuration, enables users to override the application's default node settings and connect to any Algorand node. See the Runtime Node Configuration guide for details about implementing this feature.

For a complete list of all available properties and methods, see the useNetwork API Reference.

Working with Reactive Values

The Svelte adapter uses a .current property pattern for accessing reactive values:

This pattern integrates with TanStack Store for cross-framework consistency. The .current property aligns directly with TanStack Store's reactive system (see the useStore reference) and ensures consistent behavior across all framework adapters (React, Vue, SolidJS) while providing reliable reactivity within Svelte's compilation context.

Next Steps

Last updated