useContractEvents
Hook for reading events emitted by a smart contract, including new events as they are emitted (optional).
By default, it reads all events emitted by the smart contract.
import { useContractEvents, useContract } from "@thirdweb-dev/react"; function App() { const { contract } = useContract(contractAddress); const { data, isLoading, error } = useContractEvents(contract);}
function useContractEvents( contract: RequiredParam<ValidContractInstance>, eventName?: string, options: { subscribe?: boolean; },
An object containing options to filter the events being queried.
Available options include
queryFilter
to refine which events you want to read,a boolean
subscribe
flag to subscribe to new events as they are emitted.
Example
import { useContractEvents, useContract, Web3Button,} from "@thirdweb-dev/react"; function App() { const { contract } = useContract(contractAddress); const { data, isLoading, error } = useContractEvents( contract, "MyEvent", { queryFilter: { filters: { tokenId: 123, // e.g. Only events where tokenId = 123 }, fromBlock: 0, // Events starting from this block toBlock: 100, // Events up to this block order: "asc", // Order of events ("asc" or "desc") }, subscribe: true, // Subscribe to new events }, );
let options: { subscribe?: boolean;};
let returnType: UseQueryResult< unknown>;
The hook's data
property, once loaded, contains an array of ContractEvent
objects