useWatchBlockNumber
Hook that watches for block number changes.
Import
import { useWatchBlockNumber } from 'wagmi'
Usage
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
onBlockNumber(blockNumber) {
console.log('Block number changed!', blockNumber)
},
})
}
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
Parameters
import { type UseWatchBlockNumberParameters } from 'wagmi'
chainId
config['chains'][number]['id'] | undefined
ID of chain to watch blocks at.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
chainId: 1,
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
config
Config | undefined
Config
to use instead of retrieving from the nearest WagmiProvider
.
import { useWatchBlockNumber } from 'wagmi'
import { config } from './config'
function App() {
useWatchBlockNumber({
config,
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
emitMissed
boolean
Whether or not to emit missed blocks to the callback. Defaults to false
.
Missed blocks may occur in instances where internet connection is lost, or the block time is lesser than the polling interval of the client.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
emitMissed: true,
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
emitOnBegin
boolean
Whether or not to emit the block to the callback when the subscription opens. Defaults to false
.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
emitOnBegin: true,
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
enabled
boolean
Whether or not to watch for blocks. Defaults to true
.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
enabled: false,
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
onBlockNumber
(block: Block, prevblock: Block | undefined) => void
Callback for when block changes.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
onError
((error: Error) => void) | undefined
Error thrown from getting the block.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
},
onError(error) {
console.error('Block error', error)
},
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
poll
boolean | undefined
- Whether or not to use a polling mechanism to check for new blocks instead of a WebSocket subscription.
- Defaults to
false
for WebSocket Clients, andtrue
for non-WebSocket Clients.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
}
poll: true,
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
pollingInterval
number | undefined
- Polling frequency (in milliseconds).
- Defaults to the Config's
pollingInterval
config.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
}
pollingInterval: 1_000,
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
syncConnectedChain
boolean | undefined
- Set up subscriber for connected chain changes.
- Defaults to
Config['syncConnectedChain']
.
import { useWatchBlockNumber } from 'wagmi'
function App() {
useWatchBlockNumber({
onBlockNumber(blockNumber) {
console.log('New block number', blockNumber)
}
syncConnectedChain: false,
})
}
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
Return Type
import { type UseWatchBlockNumberReturnType } from 'wagmi'
Function for cleaning up watcher.