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

3.2 KiB

title, description
title description
useBlockNumber Hook for fetching the number of the most recent block seen.

useBlockNumber

Hook for fetching the number of the most recent block seen.

Import

import { useBlockNumber } from 'wagmi'

Usage

::: code-group

import { useBlockNumber } from 'wagmi'

function App() {
  const result = useBlockNumber()
}

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

Parameters

import { type UseBlockNumberParameters } from 'wagmi'

cacheTime

number | undefined

Time in milliseconds that cached block number will remain in memory.

::: code-group

import { useBlockNumber } from 'wagmi'

function App() {
  const result = useBlockNumber({
    cacheTime: 4_000, // [!code focus]
  })
}

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

chainId

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

ID of chain to use when fetching data.

::: code-group

import { useBlockNumber } from 'wagmi'
import { mainnet } from 'wagmi/chains' // [!code focus]

function App() {
  const result = useBlockNumber({
    chainId: mainnet.id, // [!code focus]
  })
}

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

config

Config | undefined

Config to use instead of retrieving from the nearest WagmiProvider.

::: code-group

import { useBlockNumber } from 'wagmi'
import { config } from './config' // [!code focus]

function App() {
  const result = useBlockNumber({
    config, // [!code focus]
  })
}

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

scopeKey

string | undefined

Scopes the cache to a given context. Hooks that have identical context will share the same cache.

::: code-group

import { useBlockNumber } from 'wagmi'
import { config } from './config' // [!code focus]

function App() {
  const result = useBlockNumber({
    scopeKey: 'foo', // [!code focus]
  })
}

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

watch

boolean | UseWatchBlockNumberParameters | undefined

::: code-group

import { useBlockNumber } from 'wagmi'

function App() {
  const result = useBlockNumber({
    watch: true, // [!code focus]
  })
}
import { useBlockNumber } from 'wagmi'

function App() {
  const result = useBlockNumber({
    watch: { // [!code focus]
      pollingInterval: 4_000, // [!code focus]
    }, // [!code focus]
  })
}

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

Return Type

import { type UseBlockNumberReturnType } from 'wagmi'

Action