React components for the PayPal JS SDK.
Developers integrating with PayPal are expected to add the JS SDK <script>
to a website and then render components like the PayPal Buttons after the script loads. This architecture works great for simple websites but can be challenging when building single page apps.
React developers think in terms of components and not about loading external scripts from an index.html file. It’s easy to end up with a React PayPal integration that’s sub-optimal and hurts the buyer’s user experience. For example, abstracting away all the implementation details of the PayPal Buttons into a single React component is an anti-pattern because it tightly couples script loading with rendering. It’s also problematic when you need to render multiple different PayPal components that share the same global script parameters.
react-paypal-js
provides a solution to developers to abstract away complexities around loading the JS SDK. It enforces best practices by default so buyers get the best possible user experience.
Features
<PayPalScriptProvider>
component.currency
change.<PayPalButtons />
<PayPalMarks />
<PayPalMessages />
To get started, install react-paypal-js with npm.
npm install @paypal/react-paypal-js
This PayPal React library consists of two main parts:
<PayPalScriptProvider />
component manages loading the JS SDK script. Add it to the root of your React app. It uses the Context API for managing state and communicating to child components. It also supports reloading the script when parameters change.<PayPalButtons />
are used to render the UI for PayPal products served by the JS SDK.// App.js
import { PayPalScriptProvider, PayPalButtons } from "@paypal/react-paypal-js";
export default function App() {
return (
<PayPalScriptProvider options={{ "client-id": "sb" }}>
<PayPalButtons style={{ layout: "horizontal" }} />
</PayPalScriptProvider>
);
}
This library supports all popular browsers, including IE 11. It provides the same browser support as the JS SDK. Here’s the full list of supported browsers.
Author: paypal
Demo: https://paypal.github.io/react-paypal-js/
Source Code: https://github.com/paypal/react-paypal-js
#react #reactjs #javascript