Skip to content
Docs
/
Hooks
/
useContractEvent

useContractEvent

Hook for subscribing to ethers.js Contract events.

import { useContractEvent } from 'wagmi'

Usage

The following examples use the ENS Registry Contract.

import { useContractEvent } from 'wagmi'

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

Arguments

contractConfig

See useContract for more info.

eventName

Name of the event to listen to.

import { useContractEvent } from 'wagmi'

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

listener

Callback that receives event.

import { useContractEvent } from 'wagmi'

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

Configuration

once (optional)

Receive only a single event, then stop listener. Defaults to false.

import { useContractEvent } from 'wagmi'

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