Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 995 Bytes

detect-network.md

File metadata and controls

28 lines (21 loc) · 995 Bytes
description
Detect a user's network and network changes.

Detect a user's network

It's important to keep track of the user's network chain ID because all RPC requests are submitted to the currently connected network.

Use the eth_chainId RPC method to detect the chain ID of the user's current network. Listen to the chainChanged provider event to detect when the user changes networks.

For example, the following code detects a user's network and when the user changes networks:

const chainId = await provider // Or window.ethereum if you don't support EIP-6963.
  .request({ method: "eth_chainId" })

provider // Or window.ethereum if you don't support EIP-6963.
  .on("chainChanged", handleChainChanged)

function handleChainChanged(chainId) {
  // We recommend reloading the page, unless you must do otherwise.
  window.location.reload()
}