【问题标题】:React-stripe-checkout onToken pass parameter or access statereact-stripe-checkout onToken 传参或访问状态
【发布时间】:2019-11-09 14:13:44
【问题描述】:

我正在使用 react-stripe-checkout 表单来收集客户和卡信息并获取令牌。到目前为止,这工作得很好。 弹出对话框完成后,调用 onToken 函数将令牌发送到后端,问题是我需要状态中的另一个信息发送到后端,我不知道如何访问它

我无法访问 this.state

  async onToken(token){
    console.log(this.state) //Error, state undefined
    console.log('Stripe Token: ', token)
    var result = await API.post("videos", "videos/offer/"+this.state.videoid, {
      body: {'price': this.state.pricePerMin, 'token': token}
    });
  }

渲染函数:

StripeCheckout
        stripeKey=""
        name=""
        token={this.onToken}
        label={'Pay ' + this.state.price + '€ and start video analysis'}
        description={this.state.price+'€ (' + this.state.minutesToPay + ' x ' + this.state.pricePerMin + '€)'}
      amount={10 * 100} // cents
      currency="EUR"
      locale="de"
      shippingAddress={false}
      billingAddress={true}
      // Note: enabling both zipCode checks and billing or shipping address will
      // cause zipCheck to be pulled from billing address (set to shipping if none provided).
      zipCode={true}
      alipay={false} // accept Alipay (default false)
      bitcoin={false}  // accept Bitcoins (default false)
      allowRememberMe // "Remember Me" option (default true)
      />

要进行 api 调用,我需要 onToken 函数中状态的 videoid。

【问题讨论】:

    标签: node.js reactjs stripe-payments


    【解决方案1】:

    有两种方法可以解决这个问题

    一个: 改变

    async onToken(token){ 
    
    

    const onToken = async (token) => {
    

    另一个是

    token={this.onToken.bind(this)}
    

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2023-01-23
      • 2021-03-10
      • 2020-10-26
      • 2023-01-16
      • 2014-11-21
      • 2016-01-02
      • 2020-04-29
      • 1970-01-01
      相关资源
      最近更新 更多