You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yes, persistance of the previously connected wallet and account.
Feature Description
It would be nice to allow a user to choose their wallet, connect their account, refresh the page, and have it automatically selected/connected so the user does not have to be re-prompted to do so. This can be done via localStorage, and it is already in place for some wagmi items, however, this feature doesn't exists.
If so, please advise on how to achieve this - the docs are sparse here.
I had tried rolling my own implementation with a custom provider that wraps some of the API, but there are errors in the underlying packages.
The wallet field from useConnectAccount is always null on first load. It would be nice to pick this up from localStorage instead by default.
Alternative Solutions
Here's a rough implementation that throws errors due to wallet being null.
constdefaultContextValues: Web3ContextValues={account: {address: undefined,addresses: undefined,chain: undefined,chainId: undefined,connector: undefined,isConnected: false,isReconnecting: false,isConnecting: false,isDisconnected: true,status: 'disconnected',},connecting: false,isConnected: false,handleConnectDisconnect: ()=>{},}constWeb3Context=createContext<Web3ContextValues>(defaultContextValues)exportfunctionWeb3Provider({ children }: Props){constcontext=useContext(Web3OnboardContext)// Local state for context valuesconst[account,setAccount]=useState(defaultContextValues.account)// Hooks from web3-onboardconst[{ wallet, connecting },connect,disconnect]=useConnectWallet()consthandleConnectWallet=()=>{if(account?.isConnected&&wallet)disconnect(wallet)elseconnect()}constsetConnectedAccount=()=>{constwagmiConfig=context?.state.get().wagmiConfigif(!wagmiConfig)returnconstacct=getAccount(wagmiConfig)setAccount(acct)localStorage.setItem('connectedAccount',stringify(acct))}useEffect(()=>{// Check local storage for a previous connected account to persist through refreshesconststoredAccount=localStorage.getItem('connectedAccount')if(storedAccount)setAccount(JSON.parse(storedAccount))},[])useEffect(()=>{conststoredAccount=localStorage.getItem('connectedAccount')if(context&&wallet){// Connected, set local state and storageconsole.log('connected',{ context, wallet })setConnectedAccount()}elseif(storedAccount){setAccount(JSON.parse(storedAccount))}else{// Disconnected, reset local state and storageconsole.log('disconnected')setAccount(undefined)localStorage.removeItem('connectedAccount')}},[wallet,context])return(<Web3Context.Providervalue={{
account,connecting: !!account?.isConnecting,isConnected: !!account?.isConnected,handleConnectDisconnect: handleConnectWallet,}}>{children}</Web3Context.Provider>)}
Anything else?
No response
The text was updated successfully, but these errors were encountered:
Is your request related to a problem?
Yes, persistance of the previously connected wallet and account.
Feature Description
It would be nice to allow a user to choose their wallet, connect their account, refresh the page, and have it automatically selected/connected so the user does not have to be re-prompted to do so. This can be done via localStorage, and it is already in place for some wagmi items, however, this feature doesn't exists.
If so, please advise on how to achieve this - the docs are sparse here.
I had tried rolling my own implementation with a custom provider that wraps some of the API, but there are errors in the underlying packages.
The
wallet
field fromuseConnectAccount
is always null on first load. It would be nice to pick this up from localStorage instead by default.Alternative Solutions
Here's a rough implementation that throws errors due to wallet being null.
Anything else?
No response
The text was updated successfully, but these errors were encountered: