Giter Site home page Giter Site logo

miki-saarna / react-plaid-link Goto Github PK

View Code? Open in Web Editor NEW

This project forked from plaid/react-plaid-link

0.0 0.0 0.0 3.77 MB

React bindings for Plaid Link

Home Page: https://plaid.github.io/react-plaid-link

License: MIT License

Makefile 7.98% JavaScript 31.87% Shell 3.09% TypeScript 57.06%

react-plaid-link's Introduction

react-plaid-link npm version

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) => {
      // send public_token to server
    },
    []
  );

  const config: PlaidLinkOptions = {
    token,
    onSuccess,
    // onExit
    // onEvent
  };

  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);

  // generate a link_token
  React.useEffect(() => {
    async function createLinkToken() {
      let response = await fetch("/api/create_link_token");
      const { link_token } = await response.json();
      setToken(link_token);
    }
    createLinkToken();
  }, []);

  // only initialize Link once our token exists
  return token === null ? (
    // insert your loading animation here
    <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) => {
      // send public_token to server
    },
    []
  );

  const config: PlaidLinkOptions = {
    // When re-initializing Link after OAuth redirection, the same
    // Link token from the first initialization must be used
    token,
    onSuccess,
    // receivedRedirectUri: document.location.href, // required for OAuth
    // onExit
    // onEvent
  };

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

  // this opens link as soon as it's ready
  useEffect(() => {
    if (!ready) {
      return;
    }
    open();
  }, [ready, open]);

  // don't render anything, just open Link
  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);

  // generate a link_token
  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) => {
      // send public_token to server
    },
    []
  );

  // The pre-built PlaidLink component uses the usePlaidLink hook under the hood.
  // It renders a styled button element and accepts a `className` and/or `style` prop
  // to override the default styles. It accepts any Link config option as a prop such
  // as receivedRedirectUri, onEvent, onExit, onLoad, etc.
  return token === null ? (
    // insert your loading animation here
    <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.

react-plaid-link's People

Contributors

pbernasconi avatar dependabot[bot] avatar otherchen avatar aesopwolf avatar anjiren avatar jlaramie avatar cgfarmer4 avatar gilakos avatar aeidelson avatar rawrmaan avatar rblakejohnson avatar cooperjbrandon avatar subzidion avatar dcripplinger avatar dianeliu21 avatar edwinychu avatar wokkaflokka avatar hlibco avatar jclem avatar jparadasb avatar strawbee avatar jung35 avatar piton4eg avatar kulek1 avatar nas887 avatar rcbm avatar rurabe avatar timruffles avatar furcic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.