Skip to content

useChain

The useChain hook is used to get the currently connected chain as well as set the current chain.

Import

import { useChain } from "@alchemy/aa-alchemy/react";

Usage

useChain.tsx
import { useChain } from "@alchemy/aa-alchemy/react";
import { optimism } from "@alchemy/aa-core";
 
export function ComponentWithUseChain() {
  const { chain, setChain } = useChain();
 
  return (
    <div>
      <p>{chain.id}</p>
      <button onClick={() => setChain({ chain: optimism })}>
        Change Chain to Optimism
      </button>
    </div>
  );
}

Return Type

import { type UseChainResult } from "@alchemy/aa-alchemy/react";

chain

The currently connected chain.

setChain

A function that allows you to set the current chain. The chain must be included in your config's connections array.

Parameters for setChain - chain: an object of type Chain (ex. setChain({chain: Chain}))