Skip to main content
The useNetwork hook/composable provides access to network-related state and methods from the WalletManager. It enables applications to switch between networks and customize node configurations at runtime.

Basic usage

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

function NetworkStatus() {
  const { activeNetwork, activeNetworkConfig } = useNetwork()

  return (
    <div>
      <div>Network: {activeNetwork}</div>
      <div>Node: {activeNetworkConfig.algod.baseServer}</div>
    </div>
  )
}

Return values

State properties

activeNetwork
string
required
Currently active network ID (e.g., 'mainnet', 'testnet', 'localnet')
networkConfig
Record<string, NetworkConfig>
required
Complete network configuration object containing all configured networks
activeNetworkConfig
NetworkConfig
required
Configuration object for the currently active network

Methods

setActiveNetwork
function
required
Switch to a different network.
setActiveNetwork(networkId: NetworkId | string): Promise<void>
updateAlgodConfig
function
required
Update the Algod client configuration for a specific network.
updateAlgodConfig(networkId: string, config: Partial<AlgodConfig>): void
resetNetworkConfig
function
required
Reset a network’s configuration to its default values.
resetNetworkConfig(networkId: string): void

Framework-specific details

React

React provides state directly as properties:
const { activeNetwork, networkConfig } = useNetwork()

// Direct property access
console.log(activeNetwork) // 'testnet'
console.log(networkConfig['mainnet'].algod.baseServer)

Vue

Vue returns reactive refs that auto-unwrap in templates:
<script setup>
const { activeNetwork, networkConfig } = useNetwork()

// Access .value in script
console.log(activeNetwork.value) // 'testnet'
console.log(networkConfig['mainnet'].algod.baseServer)
</script>

<template>
  <!-- Auto-unwrapped in templates -->
  <div>{{ activeNetwork }}</div>
</template>

Solid

Solid uses getter functions for reactive state:
const { activeNetwork, networkConfig } = useNetwork()

// Access via function call
console.log(activeNetwork()) // 'testnet'
console.log(networkConfig()['mainnet'].algod.baseServer)

Svelte

Svelte uses .current to access reactive values:
<script lang="ts">
const { activeNetwork, networkConfig } = useNetwork()

// Access via .current property
console.log(activeNetwork.current) // 'testnet'
console.log(networkConfig['mainnet'].algod.baseServer)
</script>

<div>{activeNetwork.current}</div>

Common use cases

Switching networks

import { useNetwork, NetworkId } from '@txnlab/use-wallet-[framework]'

const { setActiveNetwork } = useNetwork()

try {
  await setActiveNetwork(NetworkId.MAINNET)
} catch (error) {
  console.error('Failed to switch network:', error)
}

Updating node configuration

import { useNetwork } from '@txnlab/use-wallet-[framework]'

const { updateAlgodConfig } = useNetwork()

// Point testnet to a custom node
updateAlgodConfig('testnet', {
  baseServer: 'https://my-custom-node.example.com',
  port: 443,
  token: 'my-api-token'
})

Resetting to defaults

import { useNetwork } from '@txnlab/use-wallet-[framework]'

const { resetNetworkConfig } = useNetwork()

// Reset testnet to default configuration
resetNetworkConfig('testnet')

Custom network configuration

// During WalletManager initialization
const manager = new WalletManager({
  wallets: [WalletId.PERA],
  network: 'custom-network',
  networkConfig: {
    'custom-network': {
      algod: {
        token: 'custom-token',
        baseServer: 'https://custom-node.example.com',
        port: 443
      },
      isTestnet: true
    }
  }
})

// Later, switch to custom network
const { setActiveNetwork } = useNetwork()
await setActiveNetwork('custom-network')

Network identifiers

Built-in network IDs available via the NetworkId enum:
import { NetworkId } from '@txnlab/use-wallet'

NetworkId.MAINNET   // 'mainnet'
NetworkId.TESTNET   // 'testnet'
NetworkId.BETANET   // 'betanet'
NetworkId.LOCALNET  // 'localnet'
You can also use custom string identifiers for custom networks.

Error handling

Network operations may throw errors in these situations:
try {
  await setActiveNetwork('invalid-network')
} catch (error) {
  // Error: Network "invalid-network" not found in network configuration
}

try {
  updateAlgodConfig('testnet', {
    baseServer: '' // Invalid empty server
  })
} catch (error) {
  // Error: Invalid configuration
}

TypeScript support

Full TypeScript definitions are available:
import type {
  NetworkId,
  NetworkConfig,
  AlgodConfig
} from '@txnlab/use-wallet-react' // or vue/solid/svelte

// NetworkConfig type
interface NetworkConfig {
  algod: AlgodConfig
  genesisHash?: string
  genesisId?: string
  isTestnet?: boolean
  caipChainId?: string
}

// AlgodConfig type
interface AlgodConfig {
  token: string | algosdk.AlgodTokenHeader | algosdk.CustomTokenHeader
  baseServer: string
  port?: string | number
  headers?: Record<string, string>
}

Build docs developers (and LLMs) love