useContractWrite
Generic hook for calling any smart contract function that requires a transaction to take place.
import { useContractWrite } from "@thirdweb-dev/react";
const { mutateAsync, isLoading, error } = useContractWrite(contract, "setName");
Usage
Provide your smart contract instance returned from the useContract
hook, along
with the name of the function you wish to call on your smart contract as arguments to the hook.
Then call the mutate
or mutateAsync
function returned by the hook, providing an array of arguments
to send to your smart contract function.
import { useContractWrite, useContract, Web3Button } from "@thirdweb-dev/react";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync, isLoading, error } = useContractWrite(
contract,
"setName",
);
return (
<Web3Button
contractAddress={contractAddress}
// Calls the "setName" function on your smart contract with "My Name" as the first argument
action={() => mutateAsync({ args: ["My Name"] })}
>
Send Transaction
</Web3Button>
);
}
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.