ethers.js/docs/v5/concepts/best-practices
2020-07-05 00:02:47 -04:00
..
index.html Updated docs build. 2020-07-05 00:02:47 -04:00
README.md Updated docs build. 2020-07-03 01:54:56 -04:00

Documentation: html

Best Practices

Network Changes

// Force page refreshes on network changes
{
    // The "any" network will allow spontaneous network changes
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
    provider.on("network", (newNetwork, oldNetwork) => {
        // When a Provider makes its initial connection, it emits a "network"
        // event with a null oldNetwork along with the newNetwork. So, if the
        // oldNetwork exists, it represents a changing network
        if (oldNetwork) {
            window.location.reload();
        }
    });
}