3

The React frontend of my dapp successfully fetches the data from the private blockchain (Ganache) and updates the states accordingly.

However, when I trigger a function via onClick that updates the states, the states render the proper values only when I refresh the page.

What's the most effective way of refreshing the states after a MetaMask transaction?

UPDATE: I was thinking that maybe there could be some way of refreshing the page via Web3js and adding that part into the promise function?

I.E. onClick -> func1.then() -> refresh

1

Are you refreshing the page in the browser (F5)?

If not, try that first.

You can also program your dapp to refresh the display when it receives a completed transaction receipt or receives an event that the dapp display is subscribed to, which is emitted by your smart contract in the transaction.


You may need to use MetaMask's network selector (in the upper left of the MetaMask popup) to switch to a different network (e.g. Localhost 8545) and then switch back to your custom Ganache network, to clear the cache.

This is not covered by the "Reset Account" button at the bottom of the Settings menu accessible via the three-line menu in the upper right.

This is a bug in MetaMask.

  • Thanks for the answer. Yes, I have mentioned that refreshing does not work. I'm on localhost 7545, and the dapp successfully interacts with Ganache by sending transactions. My problem is that I would like the dapp to refresh the states of variables once the transaction is completed. Right now, I can do that only by changing the MetaMask account. Could you please spread more light on a mechanism of listening to a transaction receipt within React? – Ruham Mar 9 '18 at 22:41
  • 1
    You can use web3 to listen for contract events. Your click handler can do an additional set of .call() actions to get new values and display them with normal Javascript. However, if bug-affected Metamask is reflecting a cached version of the blockchain, you will get events from the cached chain instead of the reset live one until you switch networks away and back OR the new chain is longer than the cached one. – WBT Mar 10 '18 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.