将反应状态传递给Paypal按钮

时间:2020-07-27 19:15:39

标签: javascript reactjs function

如何将React Component State传递给以下功能:

https://developer.paypal.com/docs/business/checkout/set-up-standard-payments/#step-1-add-payment-buttons

  componentDidMount() {
window.paypal
      .Buttons({
        createOrder: function (data, actions) {
          return actions.order.create({
            purchase_units: [
              {
                amount: {
                  value: "0.01",
                },
              },
            ],
          });
        },
        onApprove: function (data, actions) {
          return actions.order.capture().then(function (details) {
                alert("Transaction completed by " + details.payer.name.given_name);
              });
            },
          })
          .render("#paypal-button-container");
}

我想这样做:

     return actions.order.capture().then(function (details) {
        alert("Transaction completed by " + details.payer.name.given_name);
        this.callMyFunction() //<-------------------------
      });

我已经尝试使用.bind(this)

我的事件试图检测浏览器何时收到警报

无论哪种尝试都没有运气。

1 个答案:

答案 0 :(得分:0)

感谢@RobinZigmond

好像paypal函数重写了“ this”对象 所以我创建了一个局部变量const keys = ['prop', 'hi']; const obj = { foo: 'bar', prop: 'str', hi: 'hello' }; res=Object.fromEntries(Object.entries(obj).filter(o=>!keys.some(k=>o[0]==k))) console.log(res) console.log(obj) 现在我可以访问任何想要的react函数

var that = this