-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
questionAdd this to close an issue with instructions on how to repost as a question on Stack ExchangeAdd this to close an issue with instructions on how to repost as a question on Stack Exchange
Description
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
to-sh-1
Metadata
Metadata
Assignees
Labels
questionAdd this to close an issue with instructions on how to repost as a question on Stack ExchangeAdd this to close an issue with instructions on how to repost as a question on Stack Exchange