From 49579a931f5fe4a6eb4cf77b6cb0480000b26241 Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Mon, 26 Aug 2024 12:52:19 +0200 Subject: [PATCH 1/3] fix invalidate --- src/usePlaidLink.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/usePlaidLink.ts b/src/usePlaidLink.ts index 087368b3..5cd95d77 100644 --- a/src/usePlaidLink.ts +++ b/src/usePlaidLink.ts @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import useScript from 'react-script-hook'; import { createPlaid, PlaidFactory } from './factory'; @@ -29,14 +29,13 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { }); // internal state - const [plaid, setPlaid] = useState(null); const [iframeLoaded, setIframeLoaded] = useState(false); const products = ((options as PlaidLinkOptionsWithPublicKey).product || []) .slice() .sort() .join(','); - useEffect(() => { + const plaid = useMemo((): PlaidFactory | void => { // If the link.js script is still loading, return prematurely if (loading) { return; @@ -57,13 +56,7 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { return; } - // if an old plaid instance exists, destroy it before - // creating a new one - if (plaid != null) { - plaid.exit({ force: true }, () => plaid.destroy()); - } - - const next = createPlaid( + return createPlaid( { ...options, onLoad: () => { @@ -73,11 +66,6 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { }, window.Plaid.create ); - - setPlaid(next); - - // destroy the Plaid iframe factory - return () => next.exit({ force: true }, () => next.destroy()); }, [ loading, error, @@ -86,6 +74,17 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { products, ]); + useEffect(() => { + setIframeLoaded(false); + if (!plaid) { + return; + } + return () => { + // destroy the Plaid iframe factory when unmounting an instance + plaid.exit({ force: true }, () => plaid.destroy()); + } + }, [plaid]) + const ready = plaid != null && (!loading || iframeLoaded); const openNoOp = () => { From dd6fa17bb9d27277adeba9d77171ca0517d6e470 Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Mon, 26 Aug 2024 13:09:20 +0200 Subject: [PATCH 2/3] tweak --- src/usePlaidLink.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/usePlaidLink.ts b/src/usePlaidLink.ts index 5cd95d77..da136f7d 100644 --- a/src/usePlaidLink.ts +++ b/src/usePlaidLink.ts @@ -60,7 +60,6 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { { ...options, onLoad: () => { - setIframeLoaded(true); options.onLoad && options.onLoad(); }, }, @@ -85,7 +84,7 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { } }, [plaid]) - const ready = plaid != null && (!loading || iframeLoaded); + const ready = !!plaid && !loading && iframeLoaded; const openNoOp = () => { if (!options.token) { From 3e33c8b6c60851bc3e12e0a852d92f70a84ee0cd Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Mon, 26 Aug 2024 13:13:49 +0200 Subject: [PATCH 3/3] onLoad --- src/usePlaidLink.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/usePlaidLink.ts b/src/usePlaidLink.ts index da136f7d..ebe684d8 100644 --- a/src/usePlaidLink.ts +++ b/src/usePlaidLink.ts @@ -60,6 +60,7 @@ export const usePlaidLink = (options: PlaidLinkOptions) => { { ...options, onLoad: () => { + setIframeLoaded(false); options.onLoad && options.onLoad(); }, },