Create account
Sign in
Home
Payments
Business operations
Financial services
Developer tools
Security
All products
Home
Payments
Business operations
Home
Payments
Business operations
Financial services
Developer tools
Support
Overview
Quickstart
Stripe CLI
Stripe for Visual Studio Code
Webhooks
File uploads
Error handling
Error codes
API
Keys
Libraries
Upgrades
Rate limits
Card testing
Expanding responses
Domains and IP addresses
Building With Stripe
Stripe.js and Elements
React Stripe.js
Prebuilt iOS UI
Prebuilt Android UI
Extensions
Plugins
Samples
Checklist
HomeDeveloper toolsStripe.js and Elements

React Stripe.js reference

React components for Stripe.js and Stripe Elements

See the code

Want to see how React Stripe.js works or help develop it? Check out the project on GitHub.

React Stripe.js is a thin wrapper around Stripe Elements. It allows you to add Elements to any React app.

The Stripe.js reference covers complete Elements customization details.

You can use Elements with any Stripe product to collect online payments. To find the right integration path for your business, explore our docs.

This reference covers the full React Stripe.js API. If you prefer to learn by doing, check out our documentation on accepting a payment or take a look at a sample project in GitHub.

Prerequisites

This doc assumes that you already have a basic working knowledge of React and that you have already set up a React project. If you are new to React, we recommend that you take a look at the Getting Started guide before continuing.


Setup

npm umd

Install React Stripe.js and the Stripe.js loader from the npm public registry.

Terminal
npm install --save @stripe/react-stripe-js @stripe/stripe-js
npm install --save @stripe/react-stripe-js @stripe/stripe-js

We also provide a UMD build for sites that do not use npm or modules.

Include the Stripe.js script, which exports a global Stripe function, and the UMD build of React Stripe.js, which exports a global ReactStripe object. Always load the Stripe.js script directly from js.stripe.com to remain PCI compliant. Do not include the script in a bundle or host a copy of it yourself.

<!-- Stripe.js --> <script src="https://js.stripe.com/v3/"></script> <!-- React Stripe.js development build --> <script src="https://unpkg.com/@stripe/react-stripe-js@latest/dist/react-stripe.umd.js"></script> <!-- When you are ready to deploy your site to production, remove the above development script, and include the following production build. --> <script src="https://unpkg.com/@stripe/react-stripe-js@latest/dist/react-stripe.umd.min.js"></script>
<!-- Stripe.js --> <script src="https://js.stripe.com/v3/"></script> <!-- React Stripe.js development build --> <script src="https://unpkg.com/@stripe/react-stripe-js@latest/dist/react-stripe.umd.js"></script> <!-- When you are ready to deploy your site to production, remove the above development script, and include the following production build. --> <script src="https://unpkg.com/@stripe/react-stripe-js@latest/dist/react-stripe.umd.min.js"></script>

The demo in CodeSandbox lets you try out React Stripe.js without having to create a new project.

Elements provider

The Elements provider allows you to use Element components and access the Stripe object in any nested component. Render an Elements provider at the root of your React app so that it is available everywhere you need it.

To use the Elements provider, call loadStripe from @stripe/stripe-js with your publishable key. The loadStripe function will asynchronously load the Stripe.js script and initialize a Stripe object. Pass the returned Promise to Elements.

Card-Minimal.js
View full sample
import {Elements} from '@stripe/react-stripe-js'; import {loadStripe} from '@stripe/stripe-js'; // Make sure to call `loadStripe` outside of a component’s render to avoid // recreating the `Stripe` object on every render. const stripePromise = loadStripe('pk_test_JJ1eMdKN0Hp4UFJ6kWXWO4ix00jtXzq5XG'); const App = () => { return ( <Elements stripe={stripePromise}> <MyCheckoutForm /> </Elements> ); };
import {Elements} from '@stripe/react-stripe-js'; import {loadStripe} from '@stripe/stripe-js'; // Make sure to call `loadStripe` outside of a component’s render to avoid // recreating the `Stripe` object on every render. const stripePromise = loadStripe('pk_test_JJ1eMdKN0Hp4UFJ6kWXWO4ix00jtXzq5XG'); const App = () => { return ( <Elements stripe={stripePromise}> <MyCheckoutForm /> </Elements> ); };

We‘ve placed a random API key in the code. Replace it with your actual publishable API keys to test this code through your Stripe account.

Props

stripe: Stripe | null | Promise<Stripe | null>

A Stripe object or a Promise resolving to a Stripe object. The easiest way to initialize a Stripe object is with the the Stripe.js wrapper module. Once this prop has been set, it can not be changed.

You can also pass in null or a Promise resolving to null if you are performing an initial server-side render or when generating a static site.

options?: Object

Optional Elements configuration options. See available options. Once the stripe prop has been set, these options cannot be changed.

Element components

Element components provide a flexible way to securely collect payment information in your React app.

You can mount individual Element components inside of your Elements tree. Note that you can only mount one of each type of Element.

Card-Minimal.js
View full sample
import {CardElement} from '@stripe/react-stripe-js'; <CardElement options={{ style: { base: { fontSize: '16px', color: '#424770', '::placeholder': { color: '#aab7c4', }, }, invalid: { color: '#9e2146', }, }, }} />
import {CardElement} from '@stripe/react-stripe-js'; <CardElement options={{ style: { base: { fontSize: '16px', color: '#424770', '::placeholder': { color: '#aab7c4', }, }, invalid: { color: '#9e2146', }, }, }} />

Available Element components

There are many different kinds of Elements, useful for collecting different kinds of payment information. These are the available Elements today.

CardElement A flexible single-line input that collects all necessary card details.
CardNumberElement Collects the card number.
CardExpiryElement Collects the card‘s expiration date.
CardCvcElement Collects the card‘s CVC number.
PaymentRequestButtonElement An all-in-one checkout button backed by either Apple Pay or the Payment Request API. Refer to the Payment Request Button docs for more information.
AuBankAccountElement Collects Australian bank account information (BSB and account number) for use with BECS Direct Debit payments.
IbanElement The International Bank Account Number (IBAN). Available for SEPA countries.
IdealBankElement The customer's bank, for use with iDEAL payments.
FpxBankElement The customer's bank, for use with FPX payments.
AfterpayClearpayMessageElement Displays installments messaging for Afterpay payments.

Props

id?: string

Passes through to the Element’s container.

className?: string

Passes through to the Element’s container.

options?: Object

An object containing Element configuration options. See available options.

onBlur?: () => void

Triggered when the Element loses focus.

onChange?: (event: Object) => void

Triggered when data exposed by this Element is changed (e.g., when there is an error).
For more information, refer to the Stripe.js reference.

onClick?: (Object) => void

Triggered by the <PaymentRequestButtonElement> when it is clicked.
For more information, refer to the Stripe.js reference.

onFocus?: () => void

Triggered when the Element receives focus.

onReady?: (element: Element) => void

Triggered when the Element is fully rendered and can accept imperative element.focus() calls. Called with a reference to the underlying Element instance.

useElements hook

useElements(): Elements | null

To safely pass the payment information collected by an Element to the Stripe API, access the component’s underlying Element instance so that you can use it with other Stripe.js methods. If you use the React Hooks API, then useElements is the recommended and easiest way to access a mounted Element. If you need to access an Element from a class component, use ElementsConsumer instead.

Note that if you pass a Promise to the Elements provider and the Promise has not yet resolved, then useElements will return null.

Card-Minimal.js
View full sample
import {CardElement, useStripe, useElements} from '@stripe/react-stripe-js'; const CheckoutForm = () => { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event) => { // Block native form submission. event.preventDefault(); if (!stripe || !elements) { // Stripe.js has not loaded yet. Make sure to disable // form submission until Stripe.js has loaded. return; } // Get a reference to a mounted CardElement. Elements knows how // to find your CardElement because there can only ever be one of // each type of element. const cardElement = elements.getElement(CardElement); // Use your card Element with other Stripe.js APIs const {error, paymentMethod} = await stripe.createPaymentMethod({ type: 'card', card: cardElement, }); if (error) { console.log('[error]', error); } else { console.log('[PaymentMethod]', paymentMethod); } }; return ( <form onSubmit={handleSubmit}> <CardElement /> <button type="submit" disabled={!stripe}> Pay </button> </form> ); };
import {CardElement, useStripe, useElements} from '@stripe/react-stripe-js'; const CheckoutForm = () => { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event) => { // Block native form submission. event.preventDefault(); if (!stripe || !elements) { // Stripe.js has not loaded yet. Make sure to disable // form submission until Stripe.js has loaded. return; } // Get a reference to a mounted CardElement. Elements knows how // to find your CardElement because there can only ever be one of // each type of element. const cardElement = elements.getElement(CardElement); // Use your card Element with other Stripe.js APIs const {error, paymentMethod} = await stripe.createPaymentMethod({ type: 'card', card: cardElement, }); if (error) { console.log('[error]', error); } else { console.log('[PaymentMethod]', paymentMethod); } }; return ( <form onSubmit={handleSubmit}> <CardElement /> <button type="submit" disabled={!stripe}> Pay </button> </form> ); };

useStripe hook

useStripe(): Stripe | null

The useStripe hook returns a reference to the Stripe instance passed to the Elements provider. If you need to access the Stripe object from a class component, use ElementsConsumer instead.

Note that if you pass a Promise to the Elements provider and the Promise has not yet resolved, then useStripe will return null.

Card-Minimal.js
View full sample
import {CardElement, useStripe, useElements} from '@stripe/react-stripe-js'; const CheckoutForm = () => { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event) => { // Block native form submission. event.preventDefault(); if (!stripe || !elements) { // Stripe.js has not loaded yet. Make sure to disable // form submission until Stripe.js has loaded. return; } // Get a reference to a mounted CardElement. Elements knows how // to find your CardElement because there can only ever be one of // each type of element. const cardElement = elements.getElement(CardElement); // Use your card Element with other Stripe.js APIs const {error, paymentMethod} = await stripe.createPaymentMethod({ type: 'card', card: cardElement, }); if (error) { console.log('[error]', error); } else { console.log('[PaymentMethod]', paymentMethod); } }; return ( <form onSubmit={handleSubmit}> <CardElement /> <button type="submit" disabled={!stripe}> Pay </button> </form> ); };
import {CardElement, useStripe, useElements} from '@stripe/react-stripe-js'; const CheckoutForm = () => { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event) => { // Block native form submission. event.preventDefault(); if (!stripe || !elements) { // Stripe.js has not loaded yet. Make sure to disable // form submission until Stripe.js has loaded. return; } // Get a reference to a mounted CardElement. Elements knows how // to find your CardElement because there can only ever be one of // each type of element. const cardElement = elements.getElement(CardElement); // Use your card Element with other Stripe.js APIs const {error, paymentMethod} = await stripe.createPaymentMethod({ type: 'card', card: cardElement, }); if (error) { console.log('[error]', error); } else { console.log('[PaymentMethod]', paymentMethod); } }; return ( <form onSubmit={handleSubmit}> <CardElement /> <button type="submit" disabled={!stripe}> Pay </button> </form> ); };

ElementsConsumer

To safely pass the payment information collected by an Element to the Stripe API, access the component’s underlying Element instance so that you can use it with other Stripe.js methods. If you need to access the Stripe object or an Element from a class component, then ElementsConsumer provides an alternative to the useElements and useStripe hooks.

Card-Minimal.js
View full sample
import {CardElement, ElementsConsumer} from '@stripe/react-stripe-js'; class CheckoutForm extends React.Component { handleSubmit = async (event) => { // Block native form submission. event.preventDefault(); const {stripe, elements} = this.props; if (!stripe || !elements) { // Stripe.js has not loaded yet. Make sure to disable // form submission until Stripe.js has loaded. return; } // Get a reference to a mounted CardElement. Elements knows how // to find your CardElement because there can only ever be one of // each type of element. const cardElement = elements.getElement(CardElement); const {error, paymentMethod} = await stripe.createPaymentMethod({ type: 'card', card: cardElement, }); if (error) { console.log('[error]', error); } else { console.log('[PaymentMethod]', paymentMethod); } }; render() { const {stripe} = this.props; return ( <form onSubmit={this.handleSubmit}> <CardElement /> <button type="submit" disabled={!stripe}> Pay </button> </form> ); } } const InjectedCheckoutForm = () => { return ( <ElementsConsumer> {({elements, stripe}) => ( <CheckoutForm elements={elements} stripe={stripe} /> )} </ElementsConsumer> ); };
import {CardElement, ElementsConsumer} from '@stripe/react-stripe-js'; class CheckoutForm extends React.Component { handleSubmit = async (event) => { // Block native form submission. event.preventDefault(); const {stripe, elements} = this.props; if (!stripe || !elements) { // Stripe.js has not loaded yet. Make sure to disable // form submission until Stripe.js has loaded. return; } // Get a reference to a mounted CardElement. Elements knows how // to find your CardElement because there can only ever be one of // each type of element. const cardElement = elements.getElement(CardElement); const {error, paymentMethod} = await stripe.createPaymentMethod({ type: 'card', card: cardElement, }); if (error) { console.log('[error]', error); } else { console.log('[PaymentMethod]', paymentMethod); } }; render() { const {stripe} = this.props; return ( <form onSubmit={this.handleSubmit}> <CardElement /> <button type="submit" disabled={!stripe}> Pay </button> </form> ); } } const InjectedCheckoutForm = () => { return ( <ElementsConsumer> {({elements, stripe}) => ( <CheckoutForm elements={elements} stripe={stripe} /> )} </ElementsConsumer> ); };

Props

children: ({elements, stripe}) => ReactNode

This component takes a function as child. The function that you provide will be called with the Elements object that is managing your Element components and the Stripe object that you passed to <Elements>.

Note that if you pass a Promise to the Elements provider and the Promise has not yet resolved, then stripe and elements will be null.

Customization and styling

Why iframes?

We recognize that the use of iframes makes styling an Element more difficult, but they shift the burden of securely handling payment data to Stripe and provide an easy way to keep your site compliant with industry regulation.

Each element is actually mounted in an iframe under the hood. Because of this, Elements are unlikely to work with any existing styling and component frameworks that you may have. Despite this, you should still be able to fully configure an Element to seamlessly blend in with the rest of your site. Fully customizing Elements consists of configuring the Element with options, applying styles to the Element container, and responding to events.

Result CSS JavaScript
.FormGroup { margin: 0 15px 20px; padding: 0; border-style: none; background-color: #7795f8; will-change: opacity, transform; box-shadow: 0 6px 9px rgba(50, 50, 93, 0.06), 0 2px 5px rgba(0, 0, 0, 0.08), inset 0 1px 0 #829fff; border-radius: 4px; } .FormRow { display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; margin-left: 15px; border-top: 1px solid #819efc; } .StripeElement--webkit-autofill { background: transparent !important; } .StripeElement { width: 100%; padding: 11px 15px 11px 0; }
.FormGroup { margin: 0 15px 20px; padding: 0; border-style: none; background-color: #7795f8; will-change: opacity, transform; box-shadow: 0 6px 9px rgba(50, 50, 93, 0.06), 0 2px 5px rgba(0, 0, 0, 0.08), inset 0 1px 0 #829fff; border-radius: 4px; } .FormRow { display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; margin-left: 15px; border-top: 1px solid #819efc; } .StripeElement--webkit-autofill { background: transparent !important; } .StripeElement { width: 100%; padding: 11px 15px 11px 0; }
const CARD_OPTIONS = { iconStyle: 'solid', style: { base: { iconColor: '#c4f0ff', color: '#fff', fontWeight: 500, fontFamily: 'Roboto, Open Sans, Segoe UI, sans-serif', fontSize: '16px', fontSmoothing: 'antialiased', ':-webkit-autofill': {color: '#fce883'}, '::placeholder': {color: '#87bbfd'}, }, invalid: { iconColor: '#ffc7ee', color: '#ffc7ee', }, }, }; const CardField = ({onChange}) => ( <fieldset className="FormGroup"> <div className="FormRow"> <CardElement options={CARD_OPTIONS} onChange={onChange} /> </div> </fieldset> );
const CARD_OPTIONS = { iconStyle: 'solid', style: { base: { iconColor: '#c4f0ff', color: '#fff', fontWeight: 500, fontFamily: 'Roboto, Open Sans, Segoe UI, sans-serif', fontSize: '16px', fontSmoothing: 'antialiased', ':-webkit-autofill': {color: '#fce883'}, '::placeholder': {color: '#87bbfd'}, }, invalid: { iconColor: '#ffc7ee', color: '#ffc7ee', }, }, }; const CardField = ({onChange}) => ( <fieldset className="FormGroup"> <div className="FormRow"> <CardElement options={CARD_OPTIONS} onChange={onChange} /> </div> </fieldset> );

The full source code for the above example and others are available on GitHub.

Next steps

Build an integration with React Stripe.js and Elements.

  • Accept a payment
  • Adding the Payment Request Button
  • Stripe.js reference
Was this page helpful?
Questions? Contact us.
Developer tutorials on YouTube.
You can unsubscribe at any time. Read our privacy policy.
On this page
Setup
Elements provider
Element components
useElements hook
useStripe hook
ElementsConsumer
Customization and styling
Next steps