useChain

Hook for getting the Chain object of the network that the user is connected - but only if it's a supported network (added in the ThirdwebProvider 's supportedChains or one of default chains

Returns undefined if the network is not supported or the user is not connected to a wallet. You can use the useConnectionStatus hook to check if the user is connected to a wallet or not to differentiate between the two cases.

If you only want to get the chain id of the network the user is connected to regardless of whether it's supported or not, use useChainId instead.

import { useChain } from "@thirdweb-dev/react";
const chain = useChain();

Example

import { useChain, useConnectionStatus } from "@thirdweb-dev/react";
function App() {
const chain = useChain();
const status = useConnectionStatus();
if (status === "unknown") return <div> Loading... </div>;
if (status === "disconnected") return <div> disconnected </div>;
if (status === "connecting") return <div> connecting... </div>;
if (chain) {
return <p> Connected to {chain.name} </p>;
}
return <p> Connected to an unsupported network </p>;
}

Returns

An object of type Chain from @thirdweb-dev/chains package containing various information about the network, or undefined if the network is not supported or user is not connected to a wallet.