Skip to content
Documentation
/
Hooks
/
useContractEvent

useContractEvent

Hook for subscribing to Contract events.

import { useContractEvent } from 'wagmi'

Usage

The following examples use the ENS Registry Contract.

import { useContractEvent } from 'wagmi'

const App = () => {
  useContractEvent(
    {
      addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
      contractInterface: ensRegistryABI,
    },
    'NewOwner',
    (event) => console.log(event),
  )

  return ...
}

Arguments

contractConfig

See useContract for more info.

eventName

Name of event to listen to.

import { useContractEvent } from 'wagmi'

const App = () => {
  useContractEvent(
    {
      addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
      contractInterface: ensRegistryABI,
    },
    'NewResolver',
    (event) => console.log(event),
  )

  return ...
}

listener

Callback that receives event.

import { useContractEvent } from 'wagmi'

const App = () => {
  useContractEvent(
    {
      addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
      contractInterface: ensRegistryABI,
    },
    'NewOwner',
    (event) => console.log(event),
  )

  return ...
}

Configuration

once (optional)

Receive only a single event. Defaults to false.

import { useContractEvent } from 'wagmi'

const App = () => {
  useContractEvent(
    {
      addressOrName: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
      contractInterface: ensRegistryABI,
    },
    'NewOwner',
    (event) => console.log(event),
    {
      once: true,
    },
  )

  return ...
}