From 4a4f6241119db067f8e16787d0938de18b030802 Mon Sep 17 00:00:00 2001 From: yezy Date: Wed, 7 Jul 2021 16:39:53 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8FRun=20useEffect=20only=20on?= =?UTF-8?q?=20mount=20and=20unmount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useGlobalStateReducer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hooks/useGlobalStateReducer.js b/src/hooks/useGlobalStateReducer.js index 5c7d9d9..e27b8ab 100644 --- a/src/hooks/useGlobalStateReducer.js +++ b/src/hooks/useGlobalStateReducer.js @@ -42,11 +42,12 @@ function useGlobalStateReducer(reducer, globalState, config = {}) { useEffect(() => { globalState.subscribe(observer); isMounted.current = true; + return () => { globalState.unsubscribe(observer); isMounted.current = false; } - }) + }, []) function dispatch(action) { const newState = reducer(globalState.getValue(), action);