How to use onAccountsChanged?

you have to keep this syntax, where Moralis.onAccountsChanged receives a function as parameter, that is a callback function that will be called when the event happens

please post the whole code, why is it so difficult to post a working solution?

You keep posting the same stuff over and over again, when i keep telling you thats its not working
How many times more you want to post this, instead of just fixing the function i posted?

you should be able to fix that code

this is all you had to write:

Moralis.onAccountsChanged(async function(accounts) {
console.log(accounts[0]);
options = { chain: "bsc", address: accounts[0]};
const balance = await Moralis.Web3API.account.getNativeBalance(options)
  console.log(balance)
});

ok this is looking good, now we talking

So first of all, my issue is basically fixed for the code that I posted, thanks for that.

the second thing is, why is there an function(accounts) there?

And the last thing, I assume I have to paste all the code that is getting executed in the login function also into the accounts changed function?
Isnt there a better way on doing things?

Also please update your boilerplate code to work with accounts changed, cause everybody is using this and its ridiculous that there isnt already a boilerplate code for this stuff.

you can create functions so what you reuse the code in case that you don’t want to paste it twice

that function(accounts) is a callback function, a function that will be called when that event happens

I know with functions its easier but than i keep running into the problem that i cant use the output from one function as input in another function plus everything is realted to a user being logged in, so most of the time I end up writing 1 long sausage function.

I also have the issue that in my UI the user always has to refresh the page manualy and than log out, just to log back in to see the updated information for example balance or dividends or whatever it is.

These things are annoying to deal with and it should be much easier to do these things.
I hope react isnt as stupid as vanilla JS when dealing with this type of crap.
But my problem is, I dont know a lick about JS, so ihave to learn this first before getting into react,
Its frustrating to figure out on how to make this simple BS work, like account change, etc. This should come out the box already, everybody is using it so, why arent any decent tutorials to find for this stuff?
But anyways i just keep fcking rolling, there is nothing else i can do