I am working with wagmi core in svelte and am looking to make a basic wallet connect and disconnect flow.
I am able to successfully connect by pressing the connect
button, and having it call handleConnect
.
async function handleConnect() {
await connect(config, { connector: injected() })
const account = await getAccount(config)
console.log(account)
}
The console successfully shows an account.
After I do this, I can see my wallet has successfully connect.
However, upon refreshing, when checking the status of my connection, it says undefined when calling getAccount
.
onMount(async () => {
const account = await getAccount(config)
console.log(account)
})
I get the following:
{address: undefined, addresses: undefined, chain: undefined, chainId: undefined, connector: undefined, …}
Why is that?
2
Answers
Refreshing a webpage in the web browser is like restarting an ordinary program; all variables will be deleted, and then created anew. So it’s not unexpected that the previously created connection no longer exists.
You can avoid this by storing state in cookies, or localStorage, etc., and then assign variables their values based on the stored state, but then you (or the library you are using) need to write the code doing this. In this case, it seems like the library doesn’t do this for you, so that is something you would need to do yourself.
I would look into
reconnect
and creating areadable
store that useswatchAccount
. One approach would be to callreconnect
after initiating yourconfig
.For example:
Then in your svelte
Connect.svelte
component, use$
to react to theaccount
store created above. Something like this: