Docs
/Hooks
/useFeeData
useFeeData
Hook for fetching network fee information.
import { useFeeData } from 'wagmi'Usage
import { useFeeData } from 'wagmi'
function App() {
const { data, isError, isLoading } = useFeeData()
if (isLoading) return <div>Fetching fee data…</div>
if (isError) return <div>Error fetching fee data</div>
return <div>Fee data: {JSON.stringify(data?.formatted)}</div>
}Return Values
{
data?: {
gasPrice: BigNumber
maxFeePerGas: BigNumber
maxPriorityFeePerGas: BigNumber
formatted: {
gasPrice: string
maxFeePerGas: string
maxPriorityFeePerGas: string
}
}
error?: Error
isError: boolean
isFetched: boolean
isFetching: boolean
isIdle: boolean
isLoading: boolean
isRefetching: boolean
isStale: boolean
isSuccess: boolean
refetch: (options: {
throwOnError: boolean
cancelRefetch: boolean
}) => Promise<{
gasPrice: BigNumber
maxFeePerGas: BigNumber
maxPriorityFeePerGas: BigNumber
formatted: {
gasPrice: string
maxFeePerGas: string
maxPriorityFeePerGas: string
}
}>
status: 'idle' | 'error' | 'loading' | 'success'
}Configuration
formatUnits (optional)
Formats fee data using ethers.js units. Defaults to ether.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
formatUnits: 'gwei',
})
}watch (optional)
Watches and refreshes data for new blocks.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
watch: true,
})
}cacheTime (optional)
Time (in ms) which the data should remain in the cache. Defaults to 0.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
cacheTime: 2_000,
})
}enabled (optional)
Set this to false to disable this query from automatically running. Defaults to true.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
enabled: false,
})
}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 { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
staleTime: 2_000,
})
}suspense (optional)
Set this to true to enable suspense mode.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
suspense: true,
})
}onSuccess (optional)
Function to invoke when fetching new data is successful.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
onSuccess(data) {
console.log('Success', data)
},
})
}onError (optional)
Function to invoke when an error is thrown while fetching new data.
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
onError(error) {
console.log('Error', error)
},
})
}onSettled (optional)
Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).
import { useFeeData } from 'wagmi'
function App() {
const feeData = useFeeData({
onSettled(data, error) {
console.log('Settled', { data, error })
},
})
}