diff --git a/README.md b/README.md index 0815b74..1162e6d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ React state management library based on global variables and react hooks. - Support selecting deeply nested state - Support creating global state dynamically - Support both key based and non-key based global state -- States are stored as global variables(They can be used anywhere) +- States are stored as global variables(Can be used anywhere)
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);