ethers.js/docs/v5/concepts/best-practices
2020-06-12 19:21:37 -04:00
..
index.html Updated docs. 2020-06-12 19:21:37 -04:00
README.md Check-in old v5 docs changes. 2020-06-09 23:56:58 -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();
        }
    });
}