From f8275f1b87047c7c9249eafb5a7ea8972add608a Mon Sep 17 00:00:00 2001 From: Paul Phan Date: Thu, 26 Sep 2019 18:32:22 +0700 Subject: [PATCH] prevent unnecessary update --- package.json | 2 +- src/index.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5cf6a0d..bdde68f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "global-state-hook", - "version": "1.1.2", + "version": "1.1.3", "description": "~200kb for exactly what you need to manage global state", "source": "src/index.ts", "main": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index 60cca30..a06de37 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,9 +33,13 @@ export function useSubscription(subscriber: ISubscription, pick: string[] = []): }, []) React.useEffect(() => { - const updater = (nextState: State) => (!pick.length || typeof nextState !== "object" || (Object.keys(nextState).find(k => pick.includes(k)))) && setUpdate({}) + let mounted = true + const updater = (nextState: State) => mounted && (!pick.length || typeof nextState !== "object" || (Object.keys(nextState).find(k => pick.includes(k)))) && setUpdate({}) subscriber.subscribe(updater) - return () => subscriber.unsubscribe(updater) + return () => { + mounted = false + subscriber.unsubscribe(updater) + } }, []) return { state, setState } }