⚑React Quick Start

Guide for using the @txnlab/use-wallet-react package in your React project

The useWallet custom React hook provides access to the WalletManager instance and its state. It abstracts the WalletManager API and provides a simple interface for building a wallet menu and interacting with the active wallet.

circle-info

Before proceeding with this Quick Start guide, please read the Get Started section for installation and configuration instructions.

WalletProvider

In the root of your application, create a WalletManager instance with your configuration object. Wrap your app with the WalletProvider and pass the WalletManager instance as a prop.

import {
  NetworkId,
  WalletId,
  WalletManager,
  WalletProvider
} from '@txnlab/use-wallet-react'
import ReactDOM from 'react-dom/client'

// Create a manager instance
const walletManager = new WalletManager({
  wallets: [...],
  network: NetworkId.TESTNET
})

function App() {
  return (
    // Provide the manager to your App
    <WalletProvider manager={walletManager}>
      <MyApp />
    </WalletProvider>
  )
}

ReactDOM.createRoot(document.getElementById('root')!).render(<App />)

Wallet Menu

Now, in any component, you can use the useWallet hook to access the wallet manager and its state. Here is an example of a basic wallet menu implementation:

The isReady state indicates whether the wallet manager has completed initialization. It starts as false during both SSR and initial client-side mounting. During the first mount, the WalletProvider automatically attempts to restore any previously connected wallet sessions. Once this process completes, isReady switches to true.

Signing Transactions

To sign and send transactions, you can use the library's algodClient instance and the transactionSigner provided by the active wallet.

Example Applications

To see fully functioning React examples, check out these example apps:

Last updated