useTransaction
Hook for fetching transaction by hash.
import { useTransaction } from 'wagmi'
Usage
import { useTransaction } from 'wagmi'
function App() {
const { data, isError, isLoading } = useTransaction({
hash: '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
})
if (isLoading) return <div>Fetching transaction…</div>
if (isError) return <div>Error fetching transaction</div>
return <div>Transaction: {JSON.stringify(data)}</div>
}
Return Value
{
data?: TransactionResponse
error?: Error
isIdle: boolean
isLoading: boolean
isFetching: boolean
isSuccess: boolean
isError: boolean
isFetched: boolean
isFetchedAfterMount: boolean
isRefetching: boolean
refetch: (options: {
throwOnError: boolean
cancelRefetch: boolean
}) => Promise<TransactionResponse>
status: 'idle' | 'error' | 'loading' | 'success'
}
Configuration
chainId (optional)
Force a specific chain id for the request. The wagmi Client
's ethers provider
must be set up as a chain-aware function for this to work correctly.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
chainId: 1,
hash: '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
})
}
cacheTime (optional)
Time (in ms) which the data should remain in the cache. Defaults to 0
.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
cacheTime: 2_000,
})
}
enabled (optional)
Set this to false
to disable this query from automatically running. Defaults to true
.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
enabled: false,
})
}
scopeKey (optional)
Scopes the cache to a given context. Hooks that have identical context will share the same cache.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
scopeKey: 'wagmi',
})
}
staleTime (optional)
Time (in ms) after data is considered stale. If set to Infinity
the data will never be considered stale. Defaults to 0
.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
staleTime: 2_000,
})
}
suspense (optional)
Set this to true
to enable suspense mode.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
suspense: true,
})
}
onSuccess (optional)
Function to invoke when fetching new data is successful.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
onSuccess(data) {
console.log('Success', data)
},
})
}
onError (optional)
Function to invoke when an error is thrown while fetching new data.
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
onError(error) {
console.log('Error', error)
},
})
}
onSettled (optional)
Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).
import { useTransaction } from 'wagmi'
function App() {
const transaction = useTransaction({
onSettled(data, error) {
console.log('Settled', { data, error })
},
})
}