0

I want to persistent metamask connection on closing the browser window. So when I connect my dapp to metamask, close the window, and open the dapp again, I don't need to connect it again. I implemented a Similar thing using web3-react but I am facing issues with thirdweb-dev/react.

Jatin Gupta
  • 101
  • 4

1 Answers1

0

I solved that by checking the MetaMask accounts length, it returns an Array of accounts.

    const handleWalletConnect = async () => {
        try {
            const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' })
            setWallet(accounts[0])

        } catch (e) {
            console.error(e)
        }
    }

    useEffect(() => {
        window.setTimeout(() => {
            if (window.ethereum._state.accounts.length > 0) handleWalletConnect()
        }, 1000)
    }, [])