useReadContract

Signature#1

A hook to read state from a contract that automatically updates when the contract changes.

You can use raw read calls or read extensions to read from a contract.

Example

import { getContract } from "thirdweb";
import { sepolia } from "thirdweb/chains";
import { useReadContract } from "thirdweb/react";
const contract = getContract({
client,
address: "0x...",
chain: sepolia,
});
const { data, isLoading } = useReadContract({
contract,
method: "function tokenURI(uint256 tokenId) returns (string)"
params: [1n],
});
function useReadContract(
options: WithPickedOnceQueryOptions<
ReadContractOptions<TAbi, TMethod>
>,
): UseQueryResult<
ReadContractResult<PreparedMethod<ParseMethod<TAbi, TMethod>>[2]>
>;

Parameters

The options for reading from a contract

Type

let options: WithPickedOnceQueryOptions<
ReadContractOptions<TAbi, TMethod>
>;

Returns

let returnType: UseQueryResult<
ReadContractResult<PreparedMethod<ParseMethod<TAbi, TMethod>>[2]>
>;

a UseQueryResult object.

Signature#2

A hook to read state from a contract that automatically updates when the contract changes. You can use raw read calls or read extensions to read from a contract.

Example

Read a contract extension let you do complex contract queries with less code.

import { useReadContract } from "thirdweb/react";
import { getOwnedNFTs } form "thirdweb/extensions/erc721";
const { data, isLoading } = useReadContract(getOwnedNFTs, { contract, owner: address });
function useReadContract(
extension: Extension<TAbi, TParams, TResult>,
options: WithPickedOnceQueryOptions<
BaseTransactionOptions<TParams, TAbi>
>,
): UseQueryResult<TResult>;

Parameters

An extension to call.

Type

let extension: Extension<TAbi, TParams, TResult>;

The read extension params.

Type

let options: WithPickedOnceQueryOptions<
BaseTransactionOptions<TParams, TAbi>
>;

Returns

let returnType: UseQueryResult<TResult>;

a UseQueryResult object.