diff --git a/package.json b/package.json index bdde68f..e1de48f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "global-state-hook", - "version": "1.1.3", + "version": "1.1.4", "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 a06de37..02a6764 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ export interface ISubscription { } export function createSubscription(initialState: any = {}): ISubscription { - const state = initialState + const state = initialState || {} let listener: Listener[] = [] const subscribe = (fn: Listener) => listener.push(fn) const unsubscribe = (fn: Listener) => (listener = listener.filter(f => f !== fn)) @@ -28,7 +28,7 @@ export function useSubscription(subscriber: ISubscription, pick: string[] = []): const [, setUpdate] = React.useState() const setState = React.useCallback((newState: State) => { - typeof newState === "object" ? Object.assign(state, newState) : (state = newState) + typeof newState === "object" ? Object.assign(state || {}, newState) : (subscriber.state = newState) subscriber.listener.forEach(fn => fn(newState)) }, [])