Skip to main content

useReadContract

Use this hook to read public variables and get data from read-only functions of any contract.

const { data: totalCounter } = useReadContract({
abi: [YourContractABI],
address: "0xabca6bf26964af9f7eed9e03e53415d37aa90123",
functionName: "userGreetingCounter",
args: ["0xd8da6bf26964af9d7eed9e03e53415d37aa96045"],
});

This example retrieves the data returned by the userGreetingCounter function of the YourContract smart contract.

Configuration

ParameterTypeRequiredDescription
abiobject[]NoABI of the contract to read from.
addressstringNoAddress of contract to read from.
functionNamestringNoName of the function to call.
argsunknown[]NoArray of arguments to pass to the function (if accepts any). Types are inferred from contract's function parameters
enabledbooleanNoShould fetch data onMount. (default: true)
watchbooleanNoIf set to true, the data will be refetched on each block mine (default: false).
onErrorfunctionNoFunction to handle errors.

Return Values

  • The retrieved data is stored in the data property.
  • The loading state is stored in the isLoading property.
  • The error property stores any errors during reads.
  • You can refetch the data by calling the refetch function.
  • You can also use readContract function to read data. by filling the object parameter with - abi, address, functionName, args

Below is the configuration for readContract function:

Configuration

ParameterTypeRequiredDescription
abiobject[]YesABI of the contract to read from.
addressstringYesAddress of contract to read from.
functionNamestringYesName of the function to call.
argsunknown[]NoArray of arguments to pass to the function (if accepts any). Types are inferred from contract's function parameters