I use react !
useEffect(() => {
window.ethereum.on('accountsChanged', function (accounts) {
console.log(accounts[0]);
});
}, []);
It did not work. there is no console.log.
I think browser doesn't know account changed.
and is there anyway that I can notice when my wallet locked ? window.ethereum._metamask.isUnlocked() is deprecated function :(
const checkIfAccountChanged = async () => {
try {
ethereum.on('accontsChanged', accounts => {
console.log('connected', accounts[0]);
saveWalletAddress(accounts[0]);
});
} catch (error) {
console.log(error);
}
};
useEffect(() => {
checkIfAccountChanged();
}, []);
I update code !
I connect one wallet.
Hi my dapp doesn't detect when metamask account changed or locked.
I click '연결' means not connected. browser doesnt know
This is full code
const App = () => {
const { value: isLoading, setValue: setIsLoading } = useValue(true);
const { walletAddress, saveWalletAddress, clearWalletAddress } = useWalletAddressStore();
const checkIfAccountChanged = async () => {
try {
ethereum.on('accontsChanged', accounts => {
console.log('connected', accounts[0]);
saveWalletAddress(accounts[0]);
});
} catch (error) {
console.log(error);
}
};
useEffect(() => {
const walletCompany = getLocalStorage('wallet');
const connectMetamaskAuto = async () => {
const res = await connetMetamask();
res.address && saveWalletAddress(res.address);
setIsLoading(false);
};
const connectCoinbaseAuto = async () => {
const res = await connectCoinbase();
res.address && saveWalletAddress(res.address);
setIsLoading(false);
};
if (walletCompany) {
walletCompany === 'metamask' ? connectMetamaskAuto() : connectCoinbaseAuto();
} else setIsLoading(false);
}, []);
useEffect(() => {
checkIfAccountChanged();
}, []);
const { value: isLoading, setValue: setIsLoading } = useValue(true);
const [wallet, setWallet] = useState<string>('');
// const { walletAddress, saveWalletAddress, clearWalletAddress } = useWalletAddressStore();
const checkIfAccountChanged = async () => {
try {
ethereum.on('accontsChanged', accounts => {
console.log('connected', accounts[0]);
setWallet(accounts[0]);
});
} catch (error) {
console.log(error);
}
};
useEffect(() => {
const walletCompany = getLocalStorage('wallet');
const connectMetamaskAuto = async () => {
const res = await connetMetamask();
res.address && saveWalletAddress(res.address);
setIsLoading(false);
};
const connectCoinbaseAuto = async () => {
const res = await connectCoinbase();
res.address && saveWalletAddress(res.address);
setIsLoading(false);
};
if (walletCompany) {
walletCompany === 'metamask' ? connectMetamaskAuto() : connectCoinbaseAuto();
} else setIsLoading(false);
}, []);
useEffect(() => {
checkIfAccountChanged();
}, []);