Skip to main content

usetransfernft


title: useTransferNFT

displayed_sidebar: react

Hook for transferring a NFT token on a smart contract.

Available to use on smart contracts that implement the ERC721 or ERC1155 standard.

import { useTransferNFT } from "@thirdweb-dev/react";

Usage

Provide your NFT collection contract as the argument.

import { useTransferNFT, useContract, Web3Button } from "@thirdweb-dev/react";

// Your smart contract address
const contractAddress = "{{contract_address}}";

function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync: transferNft, isLoading, error } = useTransferNFT(contract);

if (error) {
console.error("failed to transfer nft", error);
}

return (
<Web3Button
contractAddress={contractAddress}
action={() =>
transferNft({
tokenId: "{{token_id}}",
to: "{{wallet_address}}", // Use useAddress hook to get current wallet address
})
}
>
Transfer NFT
</Web3Button>
);
}

Configuration

tokenId

Specify the tokenId of the NFT you want to transfer.

import { useTransferNFT, useContract, Web3Button } from "@thirdweb-dev/react";

// Your smart contract address
const contractAddress = "{{contract_address}}";

function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync: transferNft, isLoading, error } = useTransferNFT(contract);

if (error) {
console.error("failed to transfer nft", error);
}

return (
<Web3Button
contractAddress={contractAddress}
action={() =>
transferNft({
tokenId: "{{token_id}}",
to: "{{wallet_address}}", // Use useAddress hook to get current wallet address
})
}
>
Transfer NFT
</Web3Button>
);
}

to

Likely, you will want to transfer the NFT to the currently connected wallet address.

You can use the useAddress hook to get this value.

import {
useTransferNFT,
useContract,
Web3Button,
useAddress,
} from "@thirdweb-dev/react";

// Your smart contract address
const contractAddress = "{{contract_address}}";

function App() {
const address = useAddress();
const { contract } = useContract(contractAddress);
const { mutateAsync: transferNft, isLoading, error } = useTransferNFT(contract);

if (error) {
console.error("failed to transfer nft", error);
}

return (
<Web3Button
contractAddress={contractAddress}
action={() =>
transferNft({
tokenId: "{{token_id}}",
to: address, // Use useAddress hook to get current wallet address
})
}
>
Transfer NFT
</Web3Button>
);
}