useNetwork
info
This hook is deprecated. Use the useChain, useSwitchChain and useChainId hooks instead.
Hook for getting information about the current network and switching to a different network.
Returns an array value containing two elements.
An object containing the following properties:
data
object contains information about the wallet's current and supported networks.loading
indicates if the switch network request is in progress.error
holds theError
object if there was an error when attempting to switch network.
A function that can be used to switch to a different network.
import { useNetwork } from "@thirdweb-dev/react";
const [{ data, error, loading }, switchNetwork] = useNetwork();
Usage
import { useNetwork } from "@thirdweb-dev/react";
function App() {
const [{ data, error, loading }, switchNetwork] = useNetwork();
return (
<button
onClick={async () => {
if (!switchNetwork) {
console.log("can not switch network");
return;
}
const result = await switchNetwork(80001);
if (result.data) {
console.log("Switched to Mumbai testnet successfully");
} else {
console.log("Error switching to Mumbai testnet", result.error);
}
}}
>
Switch to Mumbai
</button>
);
}