Keep Wallet Connected on Page Refresh — Web3 Dev

Shmoji
2 min readFeb 1, 2022

Hello, in this tutorial I will show you how to persist a wallet connection on page reload.

To do this, you first need to know how to connect the user to a wallet on your app. You can learn that here:

Once you have an app that can connect to wallets, continue on.

Resources for this tutorial:

Implementing this is super simple. You can think of it like an algorithm with these steps:

1: On connection to wallet, set a variable to true in local storage. Keeping it in local storage will allow us to persist that data on the user’s device.

Code example:

async function connect() {
try {
await activate(injected)
localStorage.setItem('isWalletConnected', true)
} catch (ex) {
console.log(ex)
}
}

--

--