2

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

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

1 回答 1

0

您需要使用组件的状态

构造函数中:

this.state = {willShowLoader: false}

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

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

在您的发送交易功能中:(注意里面的 setState )

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

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

(this.state.willShowLoader)?<Spinner/>:<notSpinner/>
于 2018-07-24T02:58:33.147 回答