【问题标题】:Handling UI when a transaction is in pending (MetaMask)当事务处于待处理状态时处理 UI (MetaMask)
【发布时间】:2018-11-19 11:37:45
【问题描述】:

我有一个运行良好的 Metamask 付款。它由按钮的 onClick 触发。我想在交易挂起期间向用户展示一些东西,但我不知道如何因为返回的承诺已经是挖掘的交易。这是代码:

web3js.eth.sendTransaction({
                to: '0x6Dc8956E655Ccd80187265107b848D8c5B6d2459',
                from: address,
                })
                    .then(function (txHash) {
                                    console.log(txHash);
                             }).catch(error => {
                                console.log(error);
                              });
                      })

【问题讨论】:

    标签: reactjs ethereum web3js metamask


    【解决方案1】:

    你需要使用组件的状态

    构造函数中

    this.state = {willShowLoader: false}
    

    在你的onclick函数中(第二个参数是回调)

    this.state.setState({willShowLoader: true}, sendTransaction)
    

    在你的发送交易函数中:(注意then里面的setState)

    web3js.eth.sendTransaction({
                to: '0x6Dc8956E655Ccd80187265107b848D8c5B6d2459',
                from: address,
                })
                    .then(function (txHash) {
                              this.setState({willShowLoader:false})
                                    console.log(txHash);
                             }).catch(error => {
                                console.log(error);
                              });
                      })
    

    然后在你的 render 方法中:(使用条件渲染)

    (this.state.willShowLoader)?<Spinner/>:<notSpinner/>
    

    【讨论】:

    • 这会在您单击发送 tx 按钮时将 true 分配给 willShowLoader,而不是在用户签署 tx 并广播到网络时。
    猜你喜欢
    • 2014-06-21
    • 2018-03-31
    • 1970-01-01
    • 2021-10-12
    • 2020-06-02
    • 2020-05-25
    • 2014-09-20
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多