|
| 1 | +--- |
| 2 | +layout: |
| 3 | + title: |
| 4 | + visible: true |
| 5 | + description: |
| 6 | + visible: false |
| 7 | + tableOfContents: |
| 8 | + visible: true |
| 9 | + outline: |
| 10 | + visible: true |
| 11 | + pagination: |
| 12 | + visible: true |
| 13 | +--- |
| 14 | + |
| 15 | +# useNetwork |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +### Core Functionality |
| 20 | + |
| 21 | +All framework adapters provide access to the same core network state and methods, with framework-specific reactive wrappers. |
| 22 | + |
| 23 | +#### State Properties |
| 24 | + |
| 25 | +```typescript |
| 26 | +interface NetworkState { |
| 27 | + // Currently active network ID (e.g., 'mainnet', 'testnet') |
| 28 | + activeNetwork: string |
| 29 | + |
| 30 | + // Complete network configuration object |
| 31 | + networkConfig: Record<string, NetworkConfig> |
| 32 | + |
| 33 | + // Configuration for the currently active network |
| 34 | + activeNetworkConfig: NetworkConfig |
| 35 | +} |
| 36 | + |
| 37 | +interface NetworkConfig { |
| 38 | + // Algod node configuration |
| 39 | + algod: { |
| 40 | + token: string | algosdk.AlgodTokenHeader | algosdk.CustomTokenHeader |
| 41 | + baseServer: string |
| 42 | + port?: string | number |
| 43 | + headers?: Record<string, string> |
| 44 | + } |
| 45 | + |
| 46 | + // Optional network identifiers |
| 47 | + genesisHash?: string |
| 48 | + genesisId?: string |
| 49 | + isTestnet?: boolean |
| 50 | + caipChainId?: string |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +#### Methods |
| 55 | + |
| 56 | +```typescript |
| 57 | +interface NetworkMethods { |
| 58 | + // Switch to a different network |
| 59 | + setActiveNetwork(networkId: NetworkId | string): Promise<void> |
| 60 | + |
| 61 | + // Update Algod configuration for a specific network |
| 62 | + updateAlgodConfig(networkId: string, config: Partial<AlgodConfig>): void |
| 63 | + |
| 64 | + // Reset network configuration to default values |
| 65 | + resetNetworkConfig(networkId: string): void |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Framework-Specific Usage |
| 70 | + |
| 71 | +Each framework adapter provides the same functionality with patterns optimized for that framework's ecosystem. |
| 72 | + |
| 73 | +{% tabs %} |
| 74 | +{% tab title="React" %} |
| 75 | +```tsx |
| 76 | +import { useNetwork } from '@txnlab/use-wallet-react' |
| 77 | + |
| 78 | +function NetworkComponent() { |
| 79 | + const { |
| 80 | + activeNetwork, // string |
| 81 | + networkConfig, // Record<string, NetworkConfig> |
| 82 | + activeNetworkConfig, // NetworkConfig |
| 83 | + setActiveNetwork, // (networkId: string) => Promise<void> |
| 84 | + updateAlgodConfig, // (networkId: string, config: Partial<AlgodConfig>) => void |
| 85 | + resetNetworkConfig // (networkId: string) => void |
| 86 | + } = useNetwork() |
| 87 | + |
| 88 | + return ( |
| 89 | + <div> |
| 90 | + <div>Current network: {activeNetwork}</div> |
| 91 | + <div>Is testnet: {activeNetworkConfig.isTestnet ? 'Yes' : 'No'}</div> |
| 92 | + </div> |
| 93 | + ) |
| 94 | +} |
| 95 | +``` |
| 96 | +{% endtab %} |
| 97 | + |
| 98 | +{% tab title="Vue" %} |
| 99 | +```typescript |
| 100 | +<script setup> |
| 101 | +import { useNetwork } from '@txnlab/use-wallet-vue' |
| 102 | + |
| 103 | +const { |
| 104 | + activeNetwork, // Ref<string> |
| 105 | + networkConfig, // NetworkConfig object |
| 106 | + activeNetworkConfig, // ComputedRef<NetworkConfig> |
| 107 | + setActiveNetwork, // (networkId: string) => Promise<void> |
| 108 | + updateAlgodConfig, // (networkId: string, config: Partial<AlgodConfig>) => void |
| 109 | + resetNetworkConfig // (networkId: string) => void |
| 110 | +} = useNetwork() |
| 111 | +</script> |
| 112 | + |
| 113 | +<template> |
| 114 | + <div> |
| 115 | + <div>Current network: {{ activeNetwork }}</div> |
| 116 | + <div>Is testnet: {{ activeNetworkConfig.isTestnet ? 'Yes' : 'No' }}</div> |
| 117 | + </div> |
| 118 | +</template> |
| 119 | +``` |
| 120 | +{% endtab %} |
| 121 | + |
| 122 | +{% tab title="Solid" %} |
| 123 | +```tsx |
| 124 | +import { useNetwork } from '@txnlab/use-wallet-solid' |
| 125 | + |
| 126 | +function NetworkComponent() { |
| 127 | + const { |
| 128 | + activeNetwork, // () => string |
| 129 | + networkConfig, // () => Record<string, NetworkConfig> |
| 130 | + activeNetworkConfig, // () => NetworkConfig |
| 131 | + setActiveNetwork, // (networkId: string) => Promise<void> |
| 132 | + updateAlgodConfig, // (networkId: string, config: Partial<AlgodConfig>) => void |
| 133 | + resetNetworkConfig // (networkId: string) => void |
| 134 | + } = useNetwork() |
| 135 | + |
| 136 | + return ( |
| 137 | + <div> |
| 138 | + <div>Current network: {activeNetwork()}</div> |
| 139 | + <div>Is testnet: {activeNetworkConfig().isTestnet ? 'Yes' : 'No'}</div> |
| 140 | + </div> |
| 141 | + ) |
| 142 | +} |
| 143 | +``` |
| 144 | +{% endtab %} |
| 145 | +{% endtabs %} |
| 146 | + |
| 147 | +### Method Details |
| 148 | + |
| 149 | +#### setActiveNetwork |
| 150 | + |
| 151 | +```typescript |
| 152 | +setActiveNetwork(networkId: NetworkId | string): Promise<void> |
| 153 | +``` |
| 154 | + |
| 155 | +Switch to a different network. The method: |
| 156 | + |
| 157 | +* Creates a new Algod client for the target network |
| 158 | +* Updates the active network in the store |
| 159 | +* Persists the selection to local storage |
| 160 | +* Maintains active wallet sessions (if supported by the wallet) |
| 161 | + |
| 162 | +Throws an error if the network ID is not found in the configuration. |
| 163 | + |
| 164 | +#### updateAlgodConfig |
| 165 | + |
| 166 | +```typescript |
| 167 | +updateAlgodConfig(networkId: string, config: Partial<AlgodConfig>): void |
| 168 | +``` |
| 169 | + |
| 170 | +Update the Algod client configuration for a specific network. The method: |
| 171 | + |
| 172 | +* Merges the new configuration with existing settings |
| 173 | +* Creates a new Algod client if updating the active network |
| 174 | +* Persists the configuration to local storage |
| 175 | + |
| 176 | +Configuration options: |
| 177 | + |
| 178 | +```typescript |
| 179 | +interface AlgodConfig { |
| 180 | + // API token or authentication header |
| 181 | + token: string | algosdk.AlgodTokenHeader | algosdk.CustomTokenHeader |
| 182 | + |
| 183 | + // Base URL of the node |
| 184 | + baseServer: string |
| 185 | + |
| 186 | + // Optional port number |
| 187 | + port?: string | number |
| 188 | + |
| 189 | + // Optional custom headers |
| 190 | + headers?: Record<string, string> |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +#### resetNetworkConfig |
| 195 | + |
| 196 | +```typescript |
| 197 | +resetNetworkConfig(networkId: string): void |
| 198 | +``` |
| 199 | + |
| 200 | +Reset a network's configuration to its default values. The method: |
| 201 | + |
| 202 | +* Restores the original configuration for the specified network |
| 203 | +* Creates a new Algod client if resetting the active network |
| 204 | +* Removes any custom configuration from local storage |
| 205 | + |
| 206 | +### Error Handling |
| 207 | + |
| 208 | +The methods may throw errors in these situations: |
| 209 | + |
| 210 | +* Invalid network ID passed to `setActiveNetwork` |
| 211 | +* Invalid configuration passed to `updateAlgodConfig` |
| 212 | +* Network ID not found in configuration when calling `resetNetworkConfig` |
| 213 | + |
| 214 | +### TypeScript Support |
| 215 | + |
| 216 | +Full type definitions are available for all properties and methods. The types are consistent across frameworks, with framework-specific wrappers where needed (e.g., Vue refs, Solid signals). |
| 217 | + |
| 218 | +### See Also |
| 219 | + |
| 220 | +* [Network Switching Guide](../guides/switching-networks.md) - Detailed guide on network management |
| 221 | +* [Runtime Node Configuration](../guides/runtime-node-configuration.md) - Guide for customizing node configurations |
| 222 | +* [Configuration](../getting-started/configuration.md#network-configuration) - Network configuration documentation |
0 commit comments