-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (42 loc) · 1.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { useWeb3React } from "@web3-react/core"
import { useEffect } from "react"
import { injected } from "../components/wallet/connectors"
export default function Home() {
const { active, account, library, connector, activate, deactivate } = useWeb3React()
async function connect() {
try {
await activate(injected)
localStorage.setItem('isWalletConnected', true)
} catch (ex) {
console.log(ex)
}
}
async function disconnect() {
try {
deactivate()
localStorage.setItem('isWalletConnected', false)
} catch (ex) {
console.log(ex)
}
}
useEffect(() => {
const connectWalletOnPageLoad = async () => {
if (localStorage?.getItem('isWalletConnected') === 'true') {
try {
await activate(injected)
localStorage.setItem('isWalletConnected', true)
} catch (ex) {
console.log(ex)
}
}
}
connectWalletOnPageLoad()
}, [])
return (
<div className="flex flex-col items-center justify-center">
<button onClick={connect} className="py-2 mt-20 mb-4 text-lg font-bold text-white rounded-lg w-56 bg-blue-600 hover:bg-blue-800">Connect to MetaMask</button>
{active ? <span>Connected with <b>{account}</b></span> : <span>Not connected</span>}
<button onClick={disconnect} className="py-2 mt-20 mb-4 text-lg font-bold text-white rounded-lg w-56 bg-blue-600 hover:bg-blue-800">Disconnect</button>
</div>
)
}