This guide will walk you through migrating your Stripe integration from
react-stripe-elements
to
React Stripe.js.
- Prefer something a little more comprehensive? Check out the official React Stripe.js docs.
- Or take a look at some example integrations.
React Stripe.js depends on the
React Hooks API. The minimum
supported version of React is v16.8. If you use an older version, upgrade React
to use this library. If you prefer not to upgrade your React version, feel free
to continue using legacy
react-stripe-elements
.
First, use npm
or yarn
to remove react-stripe-elements
and install
@stripe/react-stripe-js
.
npm uninstall react-stripe-elements
npm install @stripe/react-stripe-js
After installing React Stripe.js, update your import statements. In places where
you used to import from react-stripe-elements
, adjust your code to import from
@stripe/react-stripe-js
.
import {CardElement} from 'react-stripe-elements';
import {CardElement} from '@stripe/react-stripe-js';
React Stripe.js no longer has a <StripeProvider>
component. Instead you will
instantiate the Stripe object
yourself and pass it directly to <Elements>
. We've prefilled the examples
below with a sample test API key. Replace it
with your own publishable key.
import {StripeProvider, Elements} from 'react-stripe-elements';
// Pass your API key to <StripeProvider> which creates and
// provides the Stripe object to <Elements>.
const App = () => (
<StripeProvider apiKey="pk_test_TYooMQauvdEDq54NiTphI7jx">
{/* Somewhere in the StripeProvider component tree... */}
<Elements>{/* Your checkout form */}</Elements>
</StripeProvider>
);
import {Elements} from '@stripe/react-stripe-js';
// Create the Stripe object yourself...
const stripe = window.Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
const App = () => (
// ...and pass it directly to <Elements>.
<Elements stripe={stripe}>{/* Your checkout form */}</Elements>
);
The way you pass in Element options is different in React Stripe.js.
import {CardElement} from 'react-stripe-elements';
<CardElement
id="my-card"
onChange={handleChange}
{/* Options are spread onto the component as props. */}
iconStyle="solid"
style={{
base: {
iconColor: '#c4f0ff',
color: '#fff',
fontSize: '16px',
},
invalid: {
iconColor: '#FFC7EE',
color: '#FFC7EE',
},
}}
/>;
import {CardElement} from '@stripe/react-stripe-js';
<CardElement
id="my-card"
onChange={handleChange}
{/* Options are passed in on their own prop. */}
options={{
iconStyle: 'solid',
style: {
base: {
iconColor: '#c4f0ff',
color: '#fff',
fontSize: '16px',
},
invalid: {
iconColor: '#FFC7EE',
color: '#FFC7EE',
},
},
}}
/>;
React Stripe.js uses hooks and consumers rather than higher order components.
import {injectStripe} from 'react-stripe-elements';
const CheckoutForm = (props) => {
const {stripe, elements} = props;
// the rest of CheckoutForm...
};
// Inject Stripe and Elements with `injectStripe`.
const InjectedCheckoutForm = injectStripe(CheckoutForm);
import {useStripe, useElements} from '@stripe/react-stripe-js';
const CheckoutForm = (props) => {
// Get a reference to Stripe or Elements using hooks.
const stripe = useStripe();
const elements = useElements();
// the rest of CheckoutForm...
};
// Or use `<ElementsConsumer>` if you do not want to use hooks.
import {ElementsConsumer} from '@stripe/react-stripe-js';
const CheckoutForm = (props) => {
const {stripe, elements} = props;
// the rest of CheckoutForm...
};
const InjectedCheckoutForm = () => (
<ElementsConsumer>
{({stripe, elements}) => (
<CheckoutForm stripe={stripe} elements={elements} />
)}
</ElementsConsumer>
);
React Stripe.js does not have the automatic Element detection.
import {injectStripe, CardElement} from 'react-stripe-elements';
const CheckoutForm = (props) => {
const {stripe, elements} = props;
const handleSubmit = (event) => {
event.preventDefault();
// Element will be inferred and is not passed to Stripe.js methods.
// e.g. stripe.createToken
stripe.createToken();
};
return (
<form onSubmit={handleSubmit}>
<CardElement />
<button>Pay</button>
</form>
);
};
const InjectedCheckoutForm = injectStripe(CheckoutForm);
import {useStripe, useElements, CardElement} from '@stripe/react-stripe-js';
const CheckoutForm = (props) => {
const stripe = useStripe();
const elements = useElements();
const handleSubmit = (event) => {
event.preventDefault();
// Use elements.getElement to get a reference to the mounted Element.
const cardElement = elements.getElement(CardElement);
// Pass the Element directly to other Stripe.js methods:
// e.g. createToken - https://stripe.com/docs/js/tokens_sources/create_token?type=cardElement
stripe.createToken(cardElement);
// or createPaymentMethod - https://stripe.com/docs/js/payment_intents/create_payment_method
stripe.createPaymentMethod({
type: 'card',
card: cardElement,
});
// or confirmCardPayment - https://stripe.com/docs/js/payment_intents/confirm_card_payment
stripe.confirmCardPayment({
payment_method: {
card: cardElement,
},
});
};
return (
<form onSubmit={handleSubmit}>
<CardElement />
<button>Pay</button>
</form>
);
};