Skip to content
Docs
/
Hooks
/
useContractEvent

useContractEvent

Hook for subscribing to ethers 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

chainId (optional)

Force a specific chain id. The wagmi Client's ethers webSocketProvider must be set up as a chain-aware function for this to work correctly.

import { useContractEvent } from 'wagmi'

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

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,
    },
  )
}