React Hook

useLaserEyes Hook

The primary React hook for integrating Bitcoin wallet functionality into your application.

The useLaserEyes hook is your gateway to Bitcoin wallet integration in React. It provides a simple, yet powerful interface to connect wallets, send transactions, and manage Bitcoin-related functionality in your application.

Basic Usage

Quick Start Example

import { useLaserEyes } from '@omnisat/lasereyes-react'
import { UNISAT } from '@omnisat/lasereyes-core'

function WalletConnect() {
  const { 
    connect, 
    disconnect, 
    connected, 
    address 
  } = useLaserEyes()

  const handleConnect = async () => {
    try {
      await connect(UNISAT)
    } catch (error) {
      console.error('Failed to connect:', error)
    }
  }

  return (
    <div>
      {connected ? (
        <div>
          <p>Connected: {address}</p>
          <button onClick={disconnect}>Disconnect</button>
        </div>
      ) : (
        <button onClick={handleConnect}>Connect Wallet</button>
      )}
    </div>
  )
}

Connection State

Available Properties

const { 
  connected,      // Boolean indicating if a wallet is connected
  connecting,     // Boolean indicating if a connection is in progress
  address,        // Connected wallet address
  publicKey,      // Connected wallet public key
  balance,        // Wallet balance in satoshis
  provider,       // Current wallet provider (e.g., UNISAT, XVERSE)
  network,        // Current network (e.g., MAINNET, TESTNET)
} = useLaserEyes()

Transaction Methods

Example: Sending BTC

const { sendBTC } = useLaserEyes()

const handleSend = async () => {
  try {
    // Convert amount to satoshis
    const satoshis = Math.floor(0.001 * 100000000)
    const txid = await sendBTC(
      'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh',
      satoshis
    )
    console.log('Transaction sent:', txid)
  } catch (error) {
    console.error('Failed to send BTC:', error)
  }
}

Error Handling

Best Practices

const { connect, sendBTC } = useLaserEyes()

// Handle specific error types
try {
  await connect(UNISAT)
} catch (err) {
  if (err.code === 'WALLET_NOT_FOUND') {
    alert('Please install UniSat wallet')
  } else if (err.code === 'USER_REJECTED') {
    alert('Connection rejected')
  } else {
    alert('Failed to connect: ' + err.message)
  }
}

// Transaction error handling
try {
  const txid = await sendBTC(recipient, amount)
  alert('Transaction sent: ' + txid)
} catch (err) {
  if (err.code === 'INSUFFICIENT_FUNDS') {
    alert('Not enough BTC')
  } else if (err.code === 'INVALID_ADDRESS') {
    alert('Invalid Bitcoin address')
  } else {
    alert('Transaction failed: ' + err.message)
  }
}

Next Steps

Guide

LaserEyesProvider

Learn how to configure the provider

Read More →
Guide

UI Components

Explore ready-to-use components

Read More →
Guide

DataSource System

Understand data providers

Read More →