Web3JS – React Re-render States After MetaMask Transaction

metamaskreactweb3js

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

Best Answer

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.

Related Topic