To detect the current network the user's wallet is connected to, use the network()
method on the window.fuel
object.
const networkInfo = await fuel.network();
console.log("Network ", networkInfo);
To add custom Network, use the wallet.addNetwork
method and pass in the Network
you want to add.
await fuel.addNetwork(network);
To listen to network events, you can use the fuel.events.network
event.
const handleNetworkChange = (network: FuelProviderConfig) => {
setNetwork(network);
};
useEffect(() => {
fuel?.on(fuel.events.network, handleNetworkChange);
return () => {
fuel?.off(fuel.events.network, handleNetworkChange);
};
}, [fuel]);
Switch the network in your wallet to see the event triggered.
In a react app you can use the useChain
hook as shown below to get information about the current chain.
import { useChain } from '@fuel-wallet/react';
// ...
const { chain } = useChain();
console.log(chain.name);
// beta-4