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

8.5 KiB

title, description
title description
useInfiniteReadContracts Hook for calling multiple read methods on a contract with "infinite scroll"/"fetch more" support.

useInfiniteReadContracts

Hook for calling multiple contract read-only methods with "infinite scrolling"/"fetch more" support.

Import

import { useInfiniteReadContracts } from 'wagmi'

Usage

The example below shows how to demonstrate how to fetch a set of mloot attributes (chestwear, footwear, and handwear) with "fetch more" support.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

const mlootContractConfig = {
  address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
  abi,
} as const

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      const args = [pageParam] as const
      return [
        { ...mlootContractConfig, functionName: 'getChest', args },
        { ...mlootContractConfig, functionName: 'getFoot', args },
        { ...mlootContractConfig, functionName: 'getHand', args },
      ]
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

In the above example, we are setting a few things:

  • cacheKey: A unique key to store the data in the cache.
  • query.initialPageParam: An initial page parameter to use when fetching the first set of contracts.
  • query.getNextPageParam: A function that returns the next page parameter to use when fetching the next set of contracts.
  • contracts: A function that provides pageParam (derived from the above) as an argument and expects to return an array of contracts.

Paginated Parameters

We can also leverage properties like getNextPageParam with a custom limit variable to achieve "pagination" of parameters. For example, we can fetch the first 10 contract functions, then fetch the next 10 contract functions, and so on.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function Example({ limit = 10 }: { limit?: number } = {}) {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      return [...new Array(limit)].map(
        (_, i) =>
          ({
            address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
            abi,
            functionName: 'getHand',
            args: [BigInt(pageParam + i)],
          }) as const,
      )
    },
    query: {
      initialPageParam: 1,
      getNextPageParam(_lastPage, _allPages, lastPageParam) {
        return lastPageParam + limit
      },
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

Parameters

import { type UseInfiniteReadContractsParameters } from 'wagmi'

cacheKey

string

A unique key to store the data in the cache.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes', // [!code hl]
    contracts(pageParam) {
      // ...
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

contracts

(pageParam: {{TPageParam}}) => Contract[]

A function that provides pageParam (derived from the above) as an argument and expects to return an array of contracts.

abi

Abi | undefined

The contract's ABI. Check out the TypeScript docs for how to set up ABIs for maximum type inference and safety.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      const args = [pageParam] as const
      return [
        // ...
        {  
          address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
          abi, // [!code hl]
          functionName: 'getChest', 
          args 
        },
        // ...
      ]
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

address

Address | undefined

The contract's address.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      const args = [pageParam] as const
      return [
        // ...
        {  
          address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df', // [!code hl]
          abi,
          functionName: 'getChest', 
          args 
        },
        // ...
      ]
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

functionName

string | undefined

  • Function to call on the contract.
  • Inferred from abi.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      const args = [pageParam] as const
      return [
        // ...
        {  
          address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
          abi,
          functionName: 'getChest', // [!code hl]
          args 
        },
        // ...
      ]
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

args

readonly unknown[] | undefined

  • Arguments to pass when calling the contract.
  • Inferred from abi and functionName.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      return [
        // ...
        {  
          address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
          abi,
          functionName: 'getChest', 
          args: [pageParam] // [!code hl]
        },
        // ...
      ]
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

chainId

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

ID of chain to use when fetching data.

::: code-group

import { useInfiniteReadContracts } from 'wagmi'
import { abi } from './abi'

function App() {
  const result = useInfiniteReadContracts({
    cacheKey: 'mlootAttributes',
    contracts(pageParam) {
      const args = [pageParam] as const
      return [
        // ...
        {  
          address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
          abi,
          functionName: 'getChest', 
          args,
          chainId: 1 // [!code hl]
        },
        // ...
      ]
    }
    query: {
      initialPageParam: 0,
      getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
        return lastPageParam + 1
      }
    }
  })
}

<<< @/snippets/abi-infinite-read.ts[abi.ts] <<< @/snippets/react/config.ts[config.ts] :::

Return Type

import { type UseInfiniteReadContractsReturnType } from 'wagmi'