clawchain/wagmi/site/react/api/hooks/useWatchBlocks.md

5.7 KiB

title, description
title description
useWatchBlocks Hook that watches for block changes.

useWatchBlocks

Hook that watches for block changes.

Import

import { useWatchBlocks } from 'wagmi'

Usage

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

Parameters

import { type UseWatchBlocksParameters } from 'wagmi'

blockTag

'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' | undefined

Block tag to watch blocks at.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    blockTag: 'latest', // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

chainId

config['chains'][number]['id'] | undefined

ID of chain to watch blocks at.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    chainId: 1, // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

config

Config | undefined

Config to use instead of retrieving from the nearest WagmiProvider.

::: code-group

import { useWatchBlocks } from 'wagmi'
import { config } from './config'

function App() {
  useWatchBlocks({
    config, // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

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.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    emitMissed: true, // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

emitOnBegin

boolean

Whether or not to emit the block to the callback when the subscription opens. Defaults to false.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    emitOnBegin: true, // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

enabled

boolean

Whether or not to watch for blocks. Defaults to true.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    enabled: false, // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

includeTransactions

boolean

Whether or not to unwrap transactions as objects (instead of hashes) in blocks. Defaults to false.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    includeTransactions: true, // [!code focus]
    onBlock(block) {
      console.log('New block', block.number)
    },
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

onBlock

(block: Block, prevblock: Block | undefined) => void

Callback for when block changes.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    onBlock(block) { // [!code focus]
      console.log('New block', block.number) // [!code focus]
    }, // [!code focus]
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

onError

((error: Error) => void) | undefined

Error thrown from getting the block.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    onBlock(block) { 
      console.log('New block', block.number) 
    }, 
    onError(error) { // [!code focus]
      console.error('Block error', error) // [!code focus]
    }, // [!code focus]
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

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, and true for non-WebSocket Clients.

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    onBlock(block) { 
      console.log('New block', block.number) 
    }
    poll: true, // [!code focus]
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

pollingInterval

number | undefined

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    onBlock(block) { 
      console.log('New block', block.number) 
    }
    pollingInterval: 1_000, // [!code focus]
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

syncConnectedChain

boolean | undefined

::: code-group

import { useWatchBlocks } from 'wagmi'

function App() {
  useWatchBlocks({
    onBlock(block) { 
      console.log('New block', block.number) 
    }
    syncConnectedChain: false, // [!code focus]
  })
}

<<< @/snippets/core/config.ts[config.ts] :::

Return Type

import { type UseWatchBlocksReturnType } from 'wagmi'

Action