反应条纹元素得到"无法读取属性'道具'未定义"

时间:2017-12-08 00:44:13

标签: javascript reactjs stripe.js react-stripe-elements

在自述文件中跟踪 react-stripe-elements 示例。我可以正确设置条纹表单,但按下付款按钮时会出现以下错误。

Uncaught TypeError: Cannot read property 'props' of undefined
    at handleSubmit (paymentForm.jsx?9f51:10)
    at HTMLUnknownElement.callCallback (react-dom.development.js?cada:540)
    at Object.invokeGuardedCallbackDev (react-dom.development.js?cada:579)
    at Object.invokeGuardedCallback (react-dom.development.js?cada:436)
....

我的paymentForm.jsx是这样的:

导入来自'的反应&#39 ;; 从' react-stripe-elements';

导入{injectStripe}

从' ./ paymentCard'

导入PaymentCard
class PaymentForm extends React.Component {

  handleSubmit(ev) {
    ev.preventDefault();
    this.props.stripe.createToken({name: 'Jenny Rosen'}).then(({token}) => {
      console.log('Received Stripe token:', token);
    });
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <PaymentCard />
        <button>Pay</button>
      </form>
    )
  }

}
export default injectStripe(PaymentForm);

1 个答案:

答案 0 :(得分:1)

通过将函数绑定到类

来解决问题
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this)
  }