Migrate from `react-stripe-elements` to React Stripe.js
masterTo migrate from the legacy react-stripe-elements library to React Stripe.js, follow these steps:
- Install dependencies: Remove
react-stripe-elementsand install@stripe/react-stripe-jsand@stripe/stripe-js. - Update imports: Change imports from
react-stripe-elementsto@stripe/react-stripe-js. - Replace
<StripeProvider>: Instead of using<StripeProvider>, useloadStripefrom@stripe/stripe-jsto create astripePromise, then pass that promise to the<Elements>component via thestripeprop. - Update Element options: Element configuration options (like
styleoriconStyle) are no longer passed as direct props. They must now be passed within anoptionsprop object. - Switch to Hooks: Replace the
injectStripeHigher Order Component with theuseStripeanduseElementshooks. Alternatively, use<ElementsConsumer>if you cannot use hooks. - Explicit Element references: React Stripe.js does not automatically detect Elements. You must use
elements.getElement(CardElement)to retrieve a reference to the mounted element and pass it explicitly to Stripe.js methods likestripe.createToken(cardElement)orstripe.createPaymentMethod({ card: cardElement, ... }).
npm uninstall react-stripe-elements
npm install @stripe/react-stripe-js @stripe/stripe-js