useContractRead
Generic hook for reading any data from a smart contract via it’s function/view/variable name.
import { useContractRead } from "@thirdweb-dev/react";
const { data, isLoading, error } = useContractRead(contract, "getName");
Usage
Provide your smart contract instance from useContract
and a function name as the arguments.
For example, to read the value of a view
on your smart contract called getName
you would do the following:
import { useContractRead, useContract } from "@thirdweb-dev/react";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress);
const { data, isLoading, error } = useContractRead(contract, "getName");
}
Generate
If you have cached the ABI of your smart contract using thirdweb generate
,
the functionName
and args
parameters are strongly typed according to your smart contract’s ABI.