watchAccount
Action for watching changes to the connected Starknet account.
Import
import { watchAccount } from "starkweb/core";
Usage
index.ts
import { watchAccount } from "starkweb/core";
import { config } from "./config";
const unwatch = watchAccount(config, {
onChange(account, prevAccount) {
console.log('Account changed:', account);
console.log('Previous account:', prevAccount);
},
});
Parameters
import { type WatchAccountParameters } from "starkweb/core";
onChange
(account: GetAccountReturnType, prevAccount: GetAccountReturnType) => void
Callback function that is called when the account changes. Receives the new account and previous account as arguments.
index.ts
import { watchAccount } from "starkweb/core";
import { config } from "./config";
const unwatch = watchAccount(config, {
onChange(account, prevAccount) {
console.log('Account changed:', account);
console.log('Previous account:', prevAccount);
},
});
Return Type
import { type WatchAccountReturnType } from "starkweb/core";
() => void
Function to stop watching for account changes.
Example
example.ts
import { watchAccount } from "starkweb/core";
import { config } from "./config";
// Start watching account changes
const unwatch = watchAccount(config, {
onChange(account, prevAccount) {
if (account.address !== prevAccount.address) {
console.log('Address changed:', account.address);
}
if (account.chainId !== prevAccount.chainId) {
console.log('Chain changed:', account.chainId);
}
},
});
// Later: stop watching
unwatch();