小编Nic*_*ico的帖子

PayPal 按钮未关闭(重新渲染后重复)

我正在使用 React 实现 PayPal 智能支付按钮,每次重新渲染我的组件时,我都会收到按钮的副本(底部的按钮包含正确的交易信息)。 在此输入图像描述

window.paypal.close()显然我需要关闭按钮,如果我尝试这样做,我会收到不是函数的错误。

我尝试遵循这个示例:Paypal React在更改金额后显示额外的按钮

这是我的代码,我使用 Redux 进行状态管理,如果购物车中的商品被删除,我需要重新渲染组件(以更新交易的商品信息):

  useEffect(() => {

  if (window.myButton) {
    window.myButton.close()
  }
  
  window.myButton = window.paypal
      .Buttons({
        createOrder: (data, actions) => {
          return actions.order.create({
            purchase_units: [
              {
                description: "test transaction",
                amount: {
                  currency_code: "USD",
                  value: document.getElementById("totalAmount").innerHTML,
                  breakdown: {
                    item_total: {
                      currency_code: "USD",
                      value: document.getElementById("totalAmount").innerHTML
                    }
                  }

                }
                ,
                items: itemsInCart.map(item => {
                  console.log(item.value)
                  return {
                    name: item.name,
                    unit_amount: {
                      currency_code: "USD",
                      value: String(item.price)
                    },
                    quantity: "1"
                  }
                })
              }
            ]
          });
        }, …
Run Code Online (Sandbox Code Playgroud)

paypal reactjs

1
推荐指数
1
解决办法
1431
查看次数

标签 统计

paypal ×1

reactjs ×1