Skip to content

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

@pictta-admin

Description

@pictta-admin
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAdd this to close an issue with instructions on how to repost as a question on Stack Exchange

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions