Once the connection is authorized, you can list the user accounts using window.fuel.accounts()
.
const accounts = await fuel.accounts();
console.log("Accounts ", accounts);
To listen to account events, you can use the fuel.events.accounts
event.
const handleAccountsEvent = (accounts: string[]) => {
setAccounts(accounts);
};
useEffect(() => {
fuel?.on(fuel.events.accounts, handleAccountsEvent);
return () => {
fuel?.off(fuel.events.accounts, handleAccountsEvent);
};
}, [fuel]);
You can also get the current account being used in the wallet using window.fuel.currentAccount()
.
const currentAccount = await fuel.currentAccount();
console.log("Current Account ", currentAccount);
To listen to current account events, you can use the fuel.events.currentAccount
event.
const handleAccountEvent = (account: string) => {
setCurrentAccount(account);
};
useEffect(() => {
// listen to the current event account, and call the handleAccountEvent
fuel?.on(fuel.events.currentAccount, handleAccountEvent);
return () => {
// remove the listener when the component is unmounted
fuel?.off(fuel.events.currentAccount, handleAccountEvent);
};
}, [fuel]);
You can keep track of the current account when using React using the useAccount
hook as shown below:
import { useAccount } from '@fuel-wallet/react';
// ...
const { accountAddress } = useAccount();
console.log('Current Account ', currentAccount);
// Current Account fuel1r20zhd...