Summary

The useSwitchNetwork hook is designed to facilitate the process of switching the active network of a wallet.

Usage

Available function and states

MethodTypeDescription
switchNetwork(args: { wallet: Wallet; network: number | string }) => Promise<void>Function to switch the given wallet to the given network id

Example

import React from "react";
import {
  useSwitchNetwork,
  useDynamicContext,
} from "@dynamic-labs/sdk-react-core";
import { base } from "viem/chains";

const NetworkSwitcher = () => {
  const switchNetwork = useSwitchNetwork();
  const { primaryWallet } = useDynamicContext();

  return (
    <>
      <button
        onClick={() =>
          switchNetwork({ wallet: primaryWallet, network: base.id })
        }
      >
        Switch to Base
      </button>
    </>
  );
};

Hook Details

Function: switchNetwork

The switchNetwork function switches the network of the given wallet. It takes an object containing the wallet whoโ€™s network should be switched, as well as the network id to switch to.