【问题标题】:React unmount a Paypal buttonReact 卸载一个 Paypal 按钮
【发布时间】:2019-07-10 04:49:44
【问题描述】:

我有一个使用 Modal 组件呈现的 Paypal 按钮。在不引发清理错误的情况下卸载 Paypal 按钮的正确方法是什么?

这是对话框的实现

<Drawer anchor="bottom" open={open} onClose={() => setStatus(false)}>
        <section className={classes.innerDrawer}>
          <h2 className={classes.innerDrawerTitle}>
            {loading ? '' : 'Checkout'}
          </h2>
          <PaypalButton
            ...props
          />
    </section>
  </Drawer>

还有按钮

const Button = paypal.Button.driver('react', { React, ReactDOM });

return (
    <Button
      env={PAYPAL_ENV}
      client={client}
      payment={(data, actions) => payment(data, actions)}
      onAuthorize={data => execute(data.payerID, data.paymentID)}
      style={{
        size: 'medium', // tiny, small, medium
        color: 'blue', // orange, blue, silver
        shape: 'rect', // pill, rect
      }}
    />
  );

我得到的错误信息:

未捕获的错误:窗口无响应 - 已清理

卸载成功时我没有收到此错误消息,这发生在我处理付款时。

链接:

https://codesandbox.io/s/r4zvkjm2kq

【问题讨论】:

  • 什么时候出现这个错误?
  • onClose 活动期间。当我尝试关闭抽屉时,它会卸载贝宝按钮
  • 我明白了。 Drawer 组件是你开发的吗?还是来自第三方库?
  • 材质ui组件

标签: reactjs paypal


【解决方案1】:

我无法重现您的问题,但我已尝试执行与您相同的代码。

在此示例中,PayPal 按钮安装在 Drawer 元素中,该元素在单击按钮后安装。当您单击抽屉外的任何位置时,抽屉将被卸载。

class Modal extends React.Component {
    constructor() {
      super()
      this.state = {
        open: false
      }
    }

    render () {      
      return (
        <div>
          <button onClick={() => this.setState({ open: true })}>Open Drawer</button>
          {
            this.state.open &&
            <Drawer anchor="left" open={true} onClose={() => this.setState({ open: false })}>
              <PayPalButton
                commit={this.state.commit}
                env={this.state.env}
                client={this.state.client}
                payment={(data, actions) => this.payment(data, actions) }
                onAuthorize={(data, actions) => this.onAuthorize(data, actions)}
                />
            </Drawer>
          }
        </div>
       )
    }
  }

工作演示:https://codepen.io/herodrigues/pen/gqQEgr

【讨论】:

  • 这样做的问题是抽屉无法控制失去其过渡效果的状态。我通过删除this.state.open &amp;&amp; 尝试了您的代码,并将其传递给打开props。错误表面。
猜你喜欢
  • 2021-03-15
  • 2018-04-08
  • 2018-05-16
  • 1970-01-01
  • 2020-06-23
  • 2022-07-08
  • 2016-11-22
  • 1970-01-01
  • 2020-09-27
相关资源
最近更新 更多