useChain
A hook that returns the current chain as well as a function to set the chain.
Note: when calling setChain
the chain that's passed in must be defined in
your initial createConfig
call. Calling setChain
causes the chain to change across the board. For example, if you use set chain then use useSmartAccountClient
, the client will flip to the loading state and address for the account on the changed chain.
For switching chains, you can also use createBundlerClient or createSmartAccoutClient directly and create a different client for each chain. You would have to manage different clients, but you wouldn't have to wait for any hooks to complete and can run these queries in parallel. This way the chain set in the config that the smart account client and other hooks inherit is not also affected.
Import
import { useChain } from "@account-kit/react";
Usage
import React from "react";
import { useChain } from "@account-kit/react";
// Assuming the chain sepolia is defined in your initial createConfig call
import { sepolia } from "@account-kit/infra";
function ComponentUsingChain() {
const { chain, setChain, isSettingChain } = useChain();
return (
<div>
<p>Current Chain: {chain.id}</p>
<button
onClick={() => setChain({ chain: sepolia })}
disabled={isSettingChain}
>
Set Chain
</button>
</div>
);
}
Parameters
mutationArgs
UseChainParams
optional properties which contain mutation arg overrides. ref
Returns
UseChainResult
an object containing the current chain and a function to set the chain as well as loading state of setting the chain. ref