我正在开发一个基于nextJS的应用,并且集成了stripe的web元素(卡片),但是这些元素没有显示出来。我在检查中看到是空的。它们在我的其他reactJS应用中工作正常,但是在这里不行,即使代码是一样的。我做错了什么?
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
async function loadStripePromise(){
const stripePromise = await loadStripe('PUBLISHABLE_KEY');
return stripePromise;
}
const AddStripeCreditCard: React.FC<AddStripeCreditCardProps> = ({
show,
close,
}) => {
return (
<CustomModal show={show} close={close} >
<Elements stripe={loadStripePromise()}>
<CardDetailsForm />
</Elements>
</CustomModal>
);
};
import { useElements, useStripe } from '@stripe/react-stripe-js';
import {
CardNumberElement,
CardCvcElement,
CardExpiryElement,
CardElement
} from "@stripe/react-stripe-js";
const CardDetailsForm = () => {
return (
<form onSubmit={handleSubmit}>
<label>
卡号
<CardNumberElement
options={options}
id='cardNumber'
/>
</label>
<label>
有效期
<CardExpiryElement
options={options}
/>
</label>
<label>
CVC
<CardCvcElement
options={options}
/>
</label>
<button type="submit" disabled={!stripe}>
支付
</button>
</form>
);
};
卡片元素没有显示出来,我也尝试了cardElement。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
问题很可能与您通过异步调用
loadStripe来初始化stripePromise有关,方法是通过调用async function loadStripePromise()我认为
loadStripe需要同步运行,以正确初始化元素。文档中有一个示例在这里const stripePromise = loadStripe(...) ... return ( <Elements stripe={stripePromise} options={options}> <CheckoutForm /> </Elements> );