Skip to content

sdk.onAccountsChanged doesn't give the latest change, but rather the previous. #889

@maxhsu-da

Description

@maxhsu-da
Image

When I click "app_provider_digital_asset", I expect that to be logged, instead, what is being logged was the previous selection, "operator"

My accounts changed function looks like this

const onAccountsChanged = useCallback((wallets: sdk.dappAPI.AccountsChangedEvent) => {
    if (wallets.length > 0) {
      const primaryWallet = wallets.find((w) => w.primary);
      if (primaryWallet) {
        setLoggedInUser((previous) => {
          if (!previous) {
            return undefined;
          }
          console.log(primaryWallet.partyId) // LOGGING HERE! 
          return {
            ...previous,
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
            userId: primaryWallet.userId,
            primaryParty: primaryWallet.partyId,
          };
        });
      }
    } else {
      throw new Error('no primary party found');
    }
  }, []);

The listener useEffect looks like this:

  React.useEffect(() => {
    const setupListeners = async () => {
      await sdk.onAccountsChanged(onAccountsChanged);
    };
    setupListeners()
      .then(() => {})
      .catch((e) => {
        console.error(e);
      });
    return () => {
      window.canton?.removeListener('accountsChanged', onAccountsChanged);
    };
  }, [loggedInUser?.primaryParty, onAccountsChanged]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions