What is react-plaid-link?
The react-plaid-link npm package is a React wrapper for Plaid Link, which is a quick and secure way to integrate with the Plaid API. It allows users to connect their bank accounts to your application, enabling functionalities like retrieving account balances, transaction histories, and more.
What are react-plaid-link's main functionalities?
Initialize Plaid Link
This code demonstrates how to initialize Plaid Link in a React application. The `PlaidLink` component is configured with necessary props like `clientName`, `env`, `product`, `publicKey`, and `onSuccess` callback.
import React from 'react';
import { PlaidLink } from 'react-plaid-link';
const App = () => {
const onSuccess = (public_token, metadata) => {
console.log('Public Token: ', public_token);
console.log('Metadata: ', metadata);
};
return (
<PlaidLink
clientName="Your App Name"
env="sandbox"
product={['auth', 'transactions']}
publicKey="your_public_key"
onSuccess={onSuccess}
>
Connect a bank account
</PlaidLink>
);
};
export default App;
Handle onExit Callback
This code demonstrates how to handle the `onExit` callback, which is triggered when the user exits the Plaid Link flow. The `onExit` callback provides error information and metadata.
import React from 'react';
import { PlaidLink } from 'react-plaid-link';
const App = () => {
const onExit = (error, metadata) => {
console.log('Error: ', error);
console.log('Metadata: ', metadata);
};
return (
<PlaidLink
clientName="Your App Name"
env="sandbox"
product={['auth', 'transactions']}
publicKey="your_public_key"
onExit={onExit}
>
Connect a bank account
</PlaidLink>
);
};
export default App;
Handle onEvent Callback
This code demonstrates how to handle the `onEvent` callback, which is triggered for various events during the Plaid Link flow. The `onEvent` callback provides the event name and metadata.
import React from 'react';
import { PlaidLink } from 'react-plaid-link';
const App = () => {
const onEvent = (eventName, metadata) => {
console.log('Event Name: ', eventName);
console.log('Metadata: ', metadata);
};
return (
<PlaidLink
clientName="Your App Name"
env="sandbox"
product={['auth', 'transactions']}
publicKey="your_public_key"
onEvent={onEvent}
>
Connect a bank account
</PlaidLink>
);
};
export default App;
Other packages similar to react-plaid-link
react-stripe-elements
The react-stripe-elements package is a React wrapper for Stripe.js, allowing you to build custom payment forms with Stripe. It provides similar functionality in terms of integrating a third-party financial service into a React application, but it focuses on payment processing rather than bank account linking.
react-paypal-button-v2
The react-paypal-button-v2 package is a React component for integrating PayPal's checkout buttons into your application. Like react-plaid-link, it simplifies the integration of a financial service, but it is specifically designed for handling PayPal transactions.
react-google-login
The react-google-login package is a React component for integrating Google login into your application. While it is not a financial service, it provides a similar user authentication flow, making it comparable in terms of user experience and integration complexity.
react-plaid-link
React hooks and components for
integrating with the Plaid Link drop module
Install
With npm
:
npm install --save react-plaid-link
With yarn
yarn add react-plaid-link
Documentation
Please refer to the official Plaid Link docs for
a more holistic understanding of the various Link options.
Examples
Head to the react-plaid-link
storybook to try out a live demo.
Using React hooks
This is the preferred approach for integrating with Plaid Link in React.
ℹ️ Note: link_token
cannot be null when passed to usePlaidLink
. Generate your link_token
outside of the component where the hook is initialized.
import React, { useCallback, useState, FunctionComponent } from "react";
import {
usePlaidLink,
PlaidLinkOptions,
PlaidLinkOnSuccess,
} from "react-plaid-link";
interface Props {
token: string;
}
const PlaidLink: FunctionComponent<Props> = ({ token }) => {
const onSuccess = useCallback<PlaidLinkOnSuccess>(
(public_token, metadata) => {
},
[]
);
const config: PlaidLinkOptions = {
token,
onSuccess,
};
const { open, ready, error } = usePlaidLink(config);
return (
<button onClick={() => open()} disabled={!ready}>
Connect a bank account
</button>
);
};
const App: FunctionComponent = () => {
const [token, setToken] = useState<string | null>(null);
React.useEffect(() => {
async function createLinkToken() {
let response = await fetch("/api/create_link_token");
const { link_token } = await response.json();
setToken(link_token);
}
createLinkToken();
}, []);
return token === null ? (
<div className="loader"></div>
) : (
<PlaidLink token={token} />
);
};
export default App;
OAuth / opening Link without a button click
Handling OAuth redirects requires opening Link without any user input. This can
also be useful if you simply if you want Link to open immediately when your page
or component renders.
import React, { useCallback, useEffect, FunctionComponent } from "react";
import {
usePlaidLink,
PlaidLinkOptions,
PlaidLinkOnSuccess,
} from "react-plaid-link";
interface Props {
token: string;
}
const OpenPlaidLink: FunctionComponent<Props> = ({ token }) => {
const onSuccess = useCallback<PlaidLinkOnSuccess>(
(public_token, metadata) => {
},
[]
);
const config: PlaidLinkOptions = {
token,
onSuccess,
};
const { open, ready, error } = usePlaidLink(config);
useEffect(() => {
if (!ready) {
return;
}
open();
}, [ready, open]);
return null;
};
export default OpenPlaidLink;
Using the pre-built component instead of the usePlaidLink hook
import React, { useCallback, useState, FunctionComponent } from "react";
import { PlaidLink, PlaidLinkOnSuccess } from "react-plaid-link";
const App: FunctionComponent = () => {
const [token, setToken] = useState<string | null>(null);
React.useEffect(() => {
async function createLinkToken() {
let response = await fetch("/api/create_link_token");
const { link_token } = await response.json();
setToken(link_token);
}
createLinkToken();
}, []);
const onSuccess = useCallback<PlaidLinkOnSuccess>(
(public_token, metadata) => {
},
[]
);
return token === null ? (
<div className="loader"></div>
) : (
<PlaidLink
token={token}
onSuccess={onSuccess}
// onExit={...}
// onEvent={...}
>
Connect a bank account
</PlaidLink>
);
};
export default App;
All available Link configuration options
Please refer to the official Plaid Link docs for
a more holistic understanding of the various Link options and the link_token
.
See also src/types/index.ts for exported types.
interface PlaidLinkOptionsWithLinkToken = {
token: string;
onSuccess: (
public_token: string,
metadata: PlaidLinkOnSuccessMetadata
) => void;
onExit?: (
error: null | PlaidLinkError,
metadata: PlaidLinkOnExitMetadata
) => void;
onEvent?: (
eventName: string,
metadata: PlaidLinkOnEventMetadata
) => void;
onLoad?: () => void;
receivedRedirectUri?: string;
}
type PlaidLinkOptions = PlaidLinkOptionsWithLinkToken;
Typescript support
TypeScript definitions for react-plaid-link
are built into the npm package. If you have previously installed @types/react-plaid-link
before this package had types, please uninstall it in favor of built-in types.