Skip to content

useChain

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

Import

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

Usage

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(optimism)}>
        Change Chain to Optimism
      </button>
    </div>
  );
}

Return Type

ts
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.