react-plaid-link
TypeScript icon, indicating that this package has built-in type declarations

3.5.1 • Public • Published

react-plaid-link npm version

React hook and components for integrating with Plaid Link

Compatibility

React 16.8+

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 Plaid Link.

Examples

Head to the react-plaid-link storybook to try out a live demo.

See the examples folder for various complete source code examples.

Using React hooks

This is the preferred approach for integrating with Plaid Link in React.

Note: token can be null initially and then set once you fetch or generate a link_token asynchronously.

ℹ️ See a full source code examples of using hooks:

import { usePlaidLink } from 'react-plaid-link';

// ...

const { open, ready } = usePlaidLink({
  token: '<GENERATED_LINK_TOKEN>',
  onSuccess: (public_token, metadata) => {
    // send public_token to server
  },
});

return (
  <button onClick={() => open()} disabled={!ready}>
    Connect a bank account
  </button>
);

ℹ️ See src/types/index.ts for exported types.

Please refer to the official Plaid Link docs for a more holistic understanding of the various Link options and the link_token.

key type
token string | null
onSuccess (public_token: string, metadata: PlaidLinkOnSuccessMetadata) => void
onExit (error: null | PlaidLinkError, metadata: PlaidLinkOnExitMetadata) => void
onEvent (eventName: PlaidLinkStableEvent | string, metadata: PlaidLinkOnEventMetadata) => void
onLoad () => void
receivedRedirectUri string | null | undefined
key type
open () => void
ready boolean
error ErrorEvent | null
exit (options?: { force: boolean }, callback?: () => void) => void

Handling OAuth redirects requires opening Link without any user input (such as clicking a button). This can also be useful if you simply want Link to open immediately when your page or component renders.

ℹ️ See full source code example at examples/oauth.tsx

import { usePlaidLink } from 'react-plaid-link';

// ...

const { open, ready } = usePlaidLink(config);

// open Link immediately when ready
React.useEffect(() => {
  if (ready) {
    open();
  }
}, [ready, open]);

return <></>;

Usage with Wallet Onboard

Plaid Wallet Onboard uses similar patterns. See the Wallet Onboard docs for a holistic understanding on configuration and usage.

ℹ️ See a full source code example of using hooks with Wallet Onboard:

import { useEthereumProvider } from 'react-plaid-link/web3';

// ...

const onSuccess = useCallback(
  async provider => {
    const accounts = await provider.request({
      method: 'eth_accounts',
    });
    setAccounts(accounts);
  },
  [setAccounts]
);

const { open, ready } = useEthereumProvider({
  token:
    'https://plaid.com/docs/wallet-onboard/add-to-app/#enable-wallet-onboard-and-retrieve-a-link-token',
  chain: {
    chainId: '0x1',
    rpcUrl: '',
  },
  onSuccess,
});

return (
  <button onClick={() => open()} disabled={!ready}>
    Connect wallet
  </button>
);

useEthereumProvider arguments

key type
token string | null
chain ChainOption
onSuccess (provider: EIP1193Provider) => void
onExit (error: null | PlaidLinkError, metadata: PlaidLinkOnExitMetadata) => void
onLoad () => void
receivedRedirectUri string | null | undefined

useEthereumProvider return value

key type
open () => void
ready boolean
error ErrorEvent | null
getCurrentEthereumProvider (chainOption: ChainOption) => Promise<EIP1193Provider | null | undefined>
isProviderActive (provider: EIP1193Provider) => Promise<boolean>
disconnectEthereumProvider (provider: EIP1193Provider) => Promise<void>

If you cannot use React hooks for legacy reasons such as incompatibility with class components, you can use the PlaidLink component.

ℹ️ See full source code example at examples/component.tsx

import { PlaidLink } from "react-plaid-link";

const App extends React.Component {
  // ...
  render() {
    return (
      <PlaidLink
        token={this.state.token}
        onSuccess={this.onSuccess}
        // onEvent={...}
        // onExit={...}
      >
        Link your bank account
      </PlaidLink>
    );
  }
}

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.

Package Sidebar

Install

npm i react-plaid-link

Weekly Downloads

176,917

Version

3.5.1

License

MIT

Unpacked Size

180 kB

Total Files

29

Last publish

Collaborators

  • clayallsopp
  • amcbride
  • timruffles
  • plaid-admin
  • andchen
  • aeidelson-plaid
  • sbrown
  • itsvs
  • tzha-plaid