【发布时间】:2020-08-01 14:38:04
【问题描述】:
在 Stripe + React 中构建付款。我在官方文档中使用了该示例,但它不起作用。付款表单有效,但提交结帐按钮时出现invalid hook call 错误。
错误:
未捕获(承诺中)错误:无效的挂钩调用。 Hooks 只能在函数组件内部调用。
代码:
import React, { Component } from "react";
import {
Elements,
useStripe,
useElements,
CardElement,
} from "@stripe/react-stripe-js";
import { stripePromise } from "../../config/stripe";
handleStripe = async (event) => {
const stripe = useStripe();
const elements = useElements();
event.preventDefault();
console.log("event", event);
const { error, paymentMethod } = await stripe.createPaymentMethod({
type: "card",
card: elements.getElement(CardElement),
});
};
render() {
return (
<Elements stripe={stripePromise}>
<CardElement />
<button
onClick={() => this.handleStripe()}
>
<span>
Checkout
</span>
</button>
</Elements>
)
}
【问题讨论】:
标签: reactjs react-hooks stripe-payments