MCPcopy Index your code
hub / github.com/Shmoji/web3-react-example / Home

Function Home

pages/index.js:5–47  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import { injected } from "../components/wallet/connectors"
4
5export default function Home() {
6 const { active, account, library, connector, activate, deactivate } = useWeb3React()
7
8 async function connect() {
9 try {
10 await activate(injected)
11 localStorage.setItem('isWalletConnected', true)
12 } catch (ex) {
13 console.log(ex)
14 }
15 }
16
17 async function disconnect() {
18 try {
19 deactivate()
20 localStorage.setItem('isWalletConnected', false)
21 } catch (ex) {
22 console.log(ex)
23 }
24 }
25
26 useEffect(() => {
27 const connectWalletOnPageLoad = async () => {
28 if (localStorage?.getItem('isWalletConnected') === 'true') {
29 try {
30 await activate(injected)
31 localStorage.setItem('isWalletConnected', true)
32 } catch (ex) {
33 console.log(ex)
34 }
35 }
36 }
37 connectWalletOnPageLoad()
38 }, [])
39
40 return (
41 <div className="flex flex-col items-center justify-center">
42 <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>
43 {active ? <span>Connected with <b>{account}</b></span> : <span>Not connected</span>}
44 <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>
45 </div>
46 )
47}

Callers

nothing calls this directly

Calls 1

connectWalletOnPageLoadFunction · 0.85

Tested by

no test coverage detected