以下是我的代码片段(正在运行)
....
let txhash = '';
class App extends Component {
....
async componentDidMount() {
.....
}
onSubmit = async(event) => {
event.preventDefault();
const accounts = await web3.eth.getAccounts();
this.setState({
message: "Waiting for the transaction to be mined..."
});
await lottery.methods.enterLotteryContract().send({
from: accounts[0],
value: web3.utils.toWei(this.state.value, 'ether')
}).on('transactionHash', function(hash) {
txhash = hash
});
this.setState({
message: "Congrats, the tx has been mined; your txhash is " + txhash
})
console.log(txhash);
});
}
更多在这里:https ://github.com/DKSukhani/lottery-react/blob/master/src/App.js
想知道是否有更好的方法让我获取txhash
然后显示它。我希望我可以将txhash
其作为状态组件之一,而不是将其声明为空变量。此外,我使用回调函数仅将其重新分配给txhash
.