【发布时间】:2021-03-01 16:56:25
【问题描述】:
这是与 Stripe 相关的代码。
据我了解,错误TypeScript 认为CheckoutForm 需要stripe 道具,但它不是通过InjectedCheckoutForm 组件提供的。
另一方面,injectStripe 应该将 props stripe 和 elements 注入到作为 elements 的子组件的包装组件中。 https://github.com/stripe/react-stripe-elements#setting-up-your-payment-form-injectstripe
type CreditCardProps = {
secret: string
}
function StripeSection(props: CreditCardProps) {
const { secret } = props
return (
<StripeProvider apiKey="pk_test_...">
<Elements>
<InjectedCheckoutForm secret={secret} />
</Elements>
</StripeProvider>
)
}
type CheckoutFormProps = {
stripe: ReactStripeElements.StripeProps
secret: string
}
function CheckoutForm(props: CheckoutFormProps) {
const { stripe, secret } = props
const handleSubmit = async (
event: React.FormEvent<HTMLFormElement>
): Promise<void> => {
event.preventDefault()
const result = await stripe.confirmCardSetup(secret, {})
}
return (
<form className="form" onSubmit={handleSubmit}>
<CardSection />
<button>Submit</button>
</form>
)
}
const InjectedCheckoutForm = injectStripe(CheckoutForm)
function CardSection() {
return (
<div>
<label>Card details</label>
<CardElement
style={{ base: { fontSize: '18px', backgroundColor: 'white' } }}
/>
</div>
)
}
我收到以下错误:
Property 'stripe' is missing in type '{ secret: string; }' but required in type 'CheckoutFormProps'.
【问题讨论】:
标签: reactjs typescript stripe-payments