Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onAccountChange is not able to listen when phantom wallet account changed #1008

Open
pictta-admin opened this issue Aug 15, 2024 · 1 comment

Comments

@pictta-admin
Copy link

import { useEffect, useState } from 'react';
import { useConnection } from '@solana/wallet-adapter-react';
import { PublicKey } from '@solana/web3.js';

const useWalletMonitor = (walletAddress) => {
    const { connection } = useConnection();
    const [accountInfo, setAccountInfo] = useState(null);

    useEffect(() => {
        if (!walletAddress) return;

        const publicKey = new PublicKey(walletAddress);

        // Function to handle account changes
        const handleAccountChange = (accountInfo) => {
            console.log('Account updated:', accountInfo);
            setAccountInfo(accountInfo);
        };

        // Subscribe to account changes
        const subscriptionId = connection.onAccountChange(publicKey, handleAccountChange);

        // Cleanup subscription on unmount
        return () => {
            connection.removeAccountChangeListener(subscriptionId);
        };
    }, [connection, walletAddress]);

    return accountInfo;
};

export default useWalletMonitor;

the connection.onAccountChange doesn't trigger when my phantom wallet account got changed, but if using the suggested way mentioned in phantom's official here https://docs.phantom.app/solana/establishing-a-connection#changing-accounts
it works

@GiovaniGuizzoYUB
Copy link

Are you using Next.js? If so, take a look at this thread: #883

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants