I'm using react to write the frontend for my dApp, and doing this to (attempt to) update react's state via an asynchronous sendTransaction
call:
var thisJS = this
web3.eth.sendTransaction({from: adminAccount, to: contractAddress, value: web3.toWei(funds,"ether")}, function(error, result) {
if (error) {
console.error(error)
} else {
console.log("Send transaction successful " + result)
const contractFunds = web3.fromWei(web3.eth.getBalance(contractAddress),"ether").toString()
thisJS.setState({contractFunds: contractFunds})
}
})
That doesn't seem to quite work as I hoped as the react setState
call doesn't (often) reflect the new funds at the contract's address. I must be doing something wrong - what's the proper way to wait for the transaction to be mined so I can correctly update react's state?