Skip to content

useTransaction

Hook for sending transaction.

import { useTransaction } from 'wagmi'

Usage

import { useTransaction } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, sendTransaction] = useTransaction({
    request: {
      to: 'awkweb.eth',
      value: BigNumber.from('1000000000000000000'), // 1 ETH
    },
  })

  if (loading) return <div>Check Wallet</div>
  if (!data)
    return (
      <button disabled={loading} onClick={async () => await sendTransaction()}>
        Send Transaction
      </button>
    )

  return (
    <div>
      {data && <div>Transaction: {JSON.stringify(data)}</div>}
      {error && <div>Error sending transaction</div>}
    </div>
  )
}

Return Values

result

{
  data?: TransactionResponse
  error?: Error
  loading?: boolean
}

sendTransaction

(config?: {
  request: TransactionRequest
}) => Promise<{ data?: string; error?: Error }>

Configuration

request (optional)

Object to use when creating transaction. See TransactionRequest for more info.

import { useTransaction } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, sendTransaction] = useTransaction({
    request: {
      to: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
      value: BigNumber.from('1000000000000000000')
    }
  })

  return ...
}