Skip to content

How to Recognize When User Has Switched Accounts in Metamask Wallet #150

Answered by tmm
seldomatt asked this question in Q&A
Discussion options

You must be logged in to vote

You can subscribe to address changes with an effect:

import * as React from "react";

import { useAccount } from "wagmi";

export const App = () => {
  const [{ data: accountData }, disconnect] = useAccount();

  React.useEffect(() => {
    console.log(`Account changed! ${accountData?.address}`);
  }, [accountData?.address]);

  return (
    <div>
      <div>{accountData.address ?? 'Not connected'}</div>
      <button onClick={disconnect}>Disconnect</button>
    </div>
  );
};

https://codesandbox.io/s/pedantic-pateu-p8iud?file=/src/App.tsx

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@seldomatt
Comment options

Answer selected by seldomatt
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants