From f2043cba31755f107a31779ed84fc0e956fb9655 Mon Sep 17 00:00:00 2001 From: Egi Ginting Date: Tue, 23 Nov 2021 17:25:10 +0700 Subject: [PATCH] fix: set state when unmounting the component Signed-off-by: Egi Ginting --- src/use-google-logout.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/use-google-logout.js b/src/use-google-logout.js index b23ed8d..8c8b570 100644 --- a/src/use-google-logout.js +++ b/src/use-google-logout.js @@ -38,6 +38,7 @@ const useGoogleLogout = ({ }, [onLogoutSuccess]) useEffect(() => { + let unmounted = false const onLoadFailure = onScriptLoadFailure || onFailure loadScript( document, @@ -60,10 +61,14 @@ const useGoogleLogout = ({ window.gapi.load('auth2', () => { if (!window.gapi.auth2.getAuthInstance()) { window.gapi.auth2.init(params).then( - () => setLoaded(true), + () => { + if (!unmounted) { + setLoaded(true) + } + }, err => onLoadFailure(err) ) - } else { + } else if (!unmounted) { setLoaded(true) } }) @@ -74,6 +79,7 @@ const useGoogleLogout = ({ ) return () => { + unmounted = true removeScript(document, 'google-login') } }, [])