useSendTransaction

A hook to send a transaction with from the user's connected wallet.

You can send a transaction with a prepared contract call , a prepared transaction , or using a write Extension .

Example

Sending a prepared contract call

import { useSendTransaction } from "thirdweb/react";
import { getContract, prepareContractCall } from "thirdweb";
import { sepolia } from "thirdweb/chains";
const contract = getContract({
address: "0x...",
chain: sepolia,
client,
});
const { mutate: sendTx, data: transactionResult } =
useSendTransaction();
const onClick = () => {
const transaction = prepareContractCall({
contract,
method: "function transfer(address to, uint256 value)",
params: [to, value],
});
sendTx(transaction);
};

Using a write extension

import { useSendTransaction } from "thirdweb/react";
import { mintTo } from "thirdweb/extensions/erc721";
const { mutate: sendTx, data: transactionResult } =
useSendTransaction();
const onClick = () => {
const transaction = mintTo({
contract,
to: "0x...",
nft: {
name: "NFT Name",
description: "NFT Description",
image: "https://example.com/image.png",
},
});
sendTx(transaction);
};

Sending a prepared transaction

import { useSendTransaction } from "thirdweb/react";
import { prepareTransaction } from "thirdweb";
import { sepolia } from "thirdweb/chains";
const { mutate: sendTx, data: transactionResult } =
useSendTransaction();
const onClick = () => {
// Send 0.1 SepoliaETH to an address
const transaction = prepareTransaction({
to: "0x...",
value: toWei("0.1"),
chain: sepolia,
client: thirdwebClient,
});
sendTx(transaction);
};
function useSendTransaction(
): UseMutationResult<
{
chain: Readonly<ChainOptions & { rpc: string }>;
client: ThirdwebClient;
maxBlocksWaitTime?: number;
readonly transactionHash: `0x${string}`;
},
Error,
>;

Parameters

Configuration for the useSendTransaction hook. Refer to SendTransactionConfig for more details.

Type

let config: {
gasless?: GaslessOptions;
};

Returns

let returnType: UseMutationResult<
{
chain: Readonly<ChainOptions & { rpc: string }>;
client: ThirdwebClient;
maxBlocksWaitTime?: number;
readonly transactionHash: `0x${string}`;
},
Error,
>;

A UseMutationResult object to send a transaction.