How to refetch and rerender balance after it changes automatically?

Hi,

I would like to know the most efficient way to automatically detect when a user native / token balance changes, refetch it and rerender the component in react?

Currently I fetch the balance in the parent component (App), then I save it in state and pass the balance into the child as prop, and I also set a refresher in the App component as a state and pass the setRefresher into the child as well.

After the user performs the withdrawal operation (in the child component), the refresher in the parent component gets updated using the setRefresher method, and therefore the parent component rerenders and refetches the balance.

But is there a better way to do it?

Would subscribing to the updates in the PolygonTransactions table be a good approach for automatic refetching? Is it possible to subscribe only to the updates of a particular address in that table? And are subscriptions performance heavy?

Thank you.

If you only re-fetch the balance when it is probably changed, thatโ€™s fine.

Would subscribing to the updates in the PolygonTransactions table be a good approach for automatic refetching?

This would be the easiest way if youโ€™re using Moralis servers.

Is it possible to subscribe only to the updates of a particular address in that table?

Donโ€™t think this is possible, you have to subscribe to the entire class/table and filter based on the address. There will be a performance hit for live queries.

Something to consider is if it actually necessary to need to subscribe to any balance changes at all times.

1 Like

Okay, thank you. I will go with the live queries then and if it hits heavily think about workarounds. Appreciate your advice. :pray: