Hi, I’m currently working with Plug wallet and trying to store the Principal during switching account. The problem is when I tried to useState to update the ID, it runs again the Connect Wallet
. How can I keep updating the Principal without having to Reconnect Wallet
twice?
Here’s my code
const [currentID, setCurrentID] = useState("NICE");
(async () => {
// Canister Ids
const nnsCanisterId = 'ryjl3-tyaaa-aaaaa-aaaba-cai'
// Whitelist
const whitelist = [
nnsCanisterId,
];
// Host
const host = "http://localhost:8080/";
// Callback to print sessionData
const onConnectionUpdate = () => {
setCurrentID(window.ic.plug.sessionManager.sessionData.principalId);
};
// Make the request
try {
const publicKey = await window.ic.plug.requestConnect({
whitelist,
host,
onConnectionUpdate,
timeout: 50000
});
console.log(`The connected user's public key is:`, publicKey);
} catch (e) {
console.log(e);
}
})();