React Stripe.js reference
Learn about React components for Stripe.js and Stripe Elements.
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.
Notiz
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 integration.
Bevor Sie loslegen
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’re new to React, we recommend that you take a look at the Getting Started guide before continuing.
Setup
Checkout provider
The CheckoutProvider
allows you to use Element components and access the Stripe object in any nested component. Render a CheckoutProvider
at the root of your React app so that it’s available everywhere you need it.
To use the CheckoutProvider
, call loadStripe from @stripe/stripe-js
with your publishable key. The loadStripe
function asynchronously loads the Stripe.js script and initializes a Stripe object. Pass the returned Promise
to the CheckoutProvider
.
import {CheckoutProvider} 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(
); export default function App() { const fetchClientSecret = () => { return fetch('/create-checkout-session', {method: 'POST'}) .then((response) => response.json()) .then((json) => json.checkoutSessionClientSecret) }; return ( <CheckoutProvider stripe={stripePromise} options={{fetchClientSecret}}> <CheckoutForm /> </CheckoutProvider> ); };'pk_test_TYooMQauvdEDq54NiTphI7jx'
prop | description |
---|---|
| required A Stripe object or a You can also pass in |
| required CheckoutProvider configuration options. See available options. You must provide the |
Element components
Element components allow you to securely collect payment information in your React app and place the Elements wherever you want on your checkout page. You can also customize the appearance.
You can mount individual Element components inside of your CheckoutProvider
tree. You can only mount one of each type of Element in a single <CheckoutProvider>
.
import {PaymentElement} from '@stripe/react-stripe-js'; const CheckoutForm = () => { return ( <form> <PaymentElement /> <button>Submit</button> </form> ); }; export default CheckoutForm;
prop | description |
---|---|
| optional An object containing Element configuration options. See available options for the Payment Element. |
| optional Triggered when the Element loses focus. |
| optional Triggered when data exposed by this Element changes. For more information, refer to the Stripe.js reference. |
| optional Triggered when the escape key is pressed within an Element. For more information, refer to the Stripe.js reference. |
| optional Triggered when the Element receives focus. |
| optional Triggered when the Element fails to load. For more information, refer to the Stripe.js reference. |
| optional Triggered when the loader UI is mounted to the DOM and ready to be displayed. You only receive these events from the For more information, refer to the Stripe.js reference. |
| optional Triggered when the Element is fully rendered and can accept imperative |
Available Element components
You can use several different kinds of Elements for collecting information on your checkout page. These are the available Elements:
Component | Usage |
---|---|
AddressElement | Collects address details for more than 236 regional formats. See the Address Element documentation to learn more. |
ExpressCheckoutElement | Allows you to accept card or wallet payments through one or more payment buttons, including Apple Pay, Google Pay, Link, or PayPal. See the Express Checkout Element documentation to learn more. |
PaymentElement | Collects payment details for more than 25 payment methods from around the globe. See the Payment Element documentation to learn more. |
useCheckout hook
useCheckout(): Checkout | null
Use the useCheckout hook in your components to get the Checkout object, which contains data from the Checkout Session, and methods to update and confirm the Session.
import {useCheckout, PaymentElement} from '@stripe/react-stripe-js'; const CheckoutForm = () => { const checkout = useCheckout(); const handleSubmit = async (event) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); const result = await checkout.confirm(); if (result.type === 'error') { // Show error to your customer (for example, payment details incomplete) console.log(result.error.message); } else { // Your customer will be redirected to your `return_url`. For some payment // methods like iDEAL, your customer will be redirected to an intermediate // site first to authorize the payment, then redirected to the `return_url`. } }; return ( <form onSubmit={handleSubmit}> <PaymentElement /> <button>Submit</button> </form> ) }; export default CheckoutForm;
Customization and styling
Each element is mounted in an iframe
, which means that Elements probably won’t work with any existing styling and component frameworks that you have. Despite this, you can still configure Elements to match the design of your site. Customizing Elements consists of responding to events and configuring Elements with the appearance option. The layout of each Element stays consistent, but you can modify colors, fonts, borders, padding, and so on.
Next steps
Build an integration with React Stripe.js and Elements with the Checkout Sessions API.