-1

here is my connect wallet function which is called when I click on connect wallet button

const connectWallet = async () => {
    if(window.ethereum){
     try{
       await window.ethereum.request({ method: "eth_requestAccounts"}).then(res=>{
        setAddress(res[0]);
        web3 = new Web3(window.ethereum)
        setWeb3(web3)
        const vm = AdmissionContract(web3)
        setVmContract(vm)       
       })
   }catch(err){
    setError(err.message)
   }
  }else{
    alert("install metamask extension!!")
  }}

Now I want a button which will call a function that disconnect this connected account

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Saira Rao
  • 1
  • 2
  • Does this answer your question? [How can I disconnect Metamask wallet using web3.js](https://stackoverflow.com/questions/72716168/how-can-i-disconnect-metamask-wallet-using-web3-js) – Yilmaz Dec 17 '22 at 14:23
  • No this is not helpful – Saira Rao Dec 17 '22 at 14:35

1 Answers1

0

You have no control over metamask. You can have another button to set the address to null

<button onClick=(()=>setAddress(null)) >disconnect</button>

if you have buttons that interact with metamask except "connect button", maybe you "pay button" you have to disable by address flag. For example

<button disabled={address} >Pay</button>

If there is address set, you let your app interact with the metamask otherwise you disable it.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202