React Plaid Link Integration

4.1.1 · active · verified Sun Apr 19

react-plaid-link is a React component and hook library designed for integrating Plaid Link into React applications. As of version 4.1.1, it provides a modern, idiomatic React interface, primarily through the `usePlaidLink` hook, to manage the Plaid Link user experience. The library abstracts away the complexities of embedding and interacting with the Plaid Link JavaScript SDK, handling initialization, callbacks, and lifecycle events. It supports a wide range of React versions, from 16.8 up to 19, demonstrating active maintenance and compatibility with the latest React ecosystems. While the README does not specify a strict release cadence, the version history suggests regular updates to maintain compatibility and incorporate new Plaid Link features. Its primary differentiator is simplifying Plaid Link integration for React developers, offering explicit callback handlers for success, exit, and events, and clear mechanisms for handling OAuth flows.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate Plaid Link using the `usePlaidLink` hook, fetching a `link_token` (simulated), and handling success, exit, and loading callbacks. It correctly disables the button until Link is ready.

import React, { useEffect, useState } from 'react';
import { usePlaidLink, PlaidLinkOnSuccessMetadata, PlaidLinkOnExitMetadata, PlaidLinkError } from 'react-plaid-link';

const MyPlaidLinkComponent: React.FC = () => {
  // In a real application, the link_token is generated server-side.
  // For this example, replace '<YOUR_GENERATED_LINK_TOKEN>' with a valid token
  // fetched from your backend. If you don't have one, Plaid offers a quickstart
  // for generating development tokens.
  const [linkToken, setLinkToken] = useState<string | null>(null);

  useEffect(() => {
    // Simulate fetching link_token from your backend
    const fetchLinkToken = async () => {
      try {
        // Example: const response = await fetch('/api/create-link-token');
        // const data = await response.json();
        // setLinkToken(data.link_token);
        // Using a placeholder for demonstration purposes
        setLinkToken('link-development-YOURPLAIDCLIENTID'); // Replace with a real token!
      } catch (error) {
        console.error('Error fetching link token:', error);
      }
    };
    fetchLinkToken();
  }, []);

  const { open, ready, error } = usePlaidLink({
    token: linkToken,
    onSuccess: (public_token: string, metadata: PlaidLinkOnSuccessMetadata) => {
      // Send the public_token to your server to exchange for an access_token
      console.log('Plaid Link successful! Public Token:', public_token, 'Metadata:', metadata);
      alert(`Public token received: ${public_token}`);
      // Typically, you'd send this to your backend:
      // fetch('/api/exchange_public_token', {
      //   method: 'POST',
      //   headers: { 'Content-Type': 'application/json' },
      //   body: JSON.stringify({ public_token, metadata }),
      // });
    },
    onExit: (error: PlaidLinkError | null, metadata: PlaidLinkOnExitMetadata) => {
      console.error('Plaid Link exited:', error, 'Metadata:', metadata);
    },
    onEvent: (eventName: string, metadata: any) => {
      console.log('Plaid Link event:', eventName, metadata);
    },
    onLoad: () => {
      console.log('Plaid Link loaded.');
    }
  });

  return (
    <div>
      <h1>Connect to Plaid</h1>
      <button onClick={() => open()} disabled={!ready || !linkToken}>
        Connect Bank Account
      </button>
      {error && <p style={{ color: 'red' }}>Error during Plaid Link initialization: {error.message}</p>}
      {!linkToken && <p>Loading Plaid Link token...</p>}
      {!ready && linkToken && <p>Plaid Link is loading and not yet ready to open.</p>}
    </div>
  );
};

export default MyPlaidLinkComponent;

view raw JSON →