Frontend
/
Switching networks

Switching networks

We make use of the useNetwork hook from wagmi to switch networks.

import { useNetwork } from 'wagmi'

const Page = () => {
  const { activeChain, switchNetwork} = useNetwork()
  const currentChainId = activeChain?.id

  // 1 => mainnet's chain ID. For other networks, see https://chainlist.org
  const desiredChainId = 1

  return (
    <div>
      {currentChainId !== desiredNetworkChainId && (
        <button onClick={() => switchNetwork(desiredChainId)}>
          Switch to mainnet
        </button>
      )}
    </div>
  )
}
Last updated on June 1, 2022