useIsAddressRole
Hook to check if a given wallet address has a specific role on a smart contract.
import { useIsAddressRole } from "@thirdweb-dev/react";
Usage
Provide your smart contract instance and a role to check for.
import { useIsAddressRole, useContract, useAddress } from "@thirdweb-dev/react";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync: mintNft, isLoading, error } = useMintNFT(contract);
const address = useAddress();
const isOwner = useIsAddressRole(contract, "owner", address);
if (error) {
console.error("failed to mint nft", error);
}
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
mintNft({
metadata: {
name: "My NFT",
description: "This is my NFT",
image: "ipfs://example.com/my-nft.png", // Accepts any URL or File type
},
to: address, // Use useAddress hook to get current wallet address
})
}
disabled={!isOwner}
>
Mint NFT
</Web3Button>
);
}
Configuration
The role
argument must be a string or a role key from the smart contract's roles
array.
The walletAddress
argument must be a valid wallet address.
You can use the useAddress
hook to get this value.
import {
useIsAddressRole,
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: mintNft, isLoading, error } = useMintNFT(contract);
const isOwner = useIsAddressRole(contract, "owner", address);
if (error) {
console.error("failed to mint nft", error);
}
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
mintNft({
metadata: {
name: "My NFT",
description: "This is my NFT",
image: "ipfs://example.com/my-nft.png", // Accepts any URL or File type
},
to: address,
})
}
disabled={!isOwner}
>
Mint NFT
</Web3Button>
);
}