Skip to content

useContractRead

Hook for calling a Contract read-only method.

import { useContractRead } from 'wagmi'

Usage

The following examples use the WAGMIGOTCHI Contract.

import { useContractRead } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, read] = useContractRead(
    {
      addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      contractInterface: wagmigotchiABI,
    },
    'getHunger',
  )

  return ...
}

Return Values

result

{
  data?: Result
  error?: Error
  loading?: boolean
}

read

(
  config: {
    args: any | any[]
    overrides?: Overrides
  } = {},
) => Promise<{ data?: Result; error?: Error }>

Arguments

contractConfig

See useContract for more info.

functionName

Name of function to call.

import { useContractRead } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, read] = useContractRead(
    {
      addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      contractInterface: wagmigotchiABI,
    },
    'getSleep',
  )

  return ...
}

Configuration

args (optional)

Arguments to pass to function call. Accepts any | any[].

⚠️

When using a list of positional arguments, you probably want to memoize them with something like React.useMemo so the updater effect remains stable.

import { useContractRead } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, read] = useContractRead(
    {
      addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      contractInterface: wagmigotchiABI,
    },
    'love',
    {
      args: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    }
  )

  return ...
}

overrides (optional)

Overrides to pass to function call.

import { useContractRead } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, read] = useContractRead(
    {
      addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      contractInterface: wagmigotchiABI,
    },
    'getHunger',
    {
      overrides: { from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' }
    }
  )

  return ...
}

skip (optional)

Skips automatically fetching data on mount. Defaults to false. Useful if you want to call read manually at some other point.

import { useContractRead } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, read] = useContractRead(
    {
      addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      contractInterface: wagmigotchiABI,
    },
    'getHunger',
    {
      skip: true,
    }
  )

  return ...
}

watch (optional)

Watches and refreshes data for new blocks.

import { useContractRead } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, read] = useContractRead(
    {
      addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      contractInterface: wagmigotchiABI,
    },
    'getHunger',
    {
      watch: true,
    }
  )

  return ...
}