From 3b1af15428ee3680485c720452dae7ab6aff7050 Mon Sep 17 00:00:00 2001 From: Jakob Langdal Date: Tue, 11 May 2021 15:13:43 +0200 Subject: [PATCH] Always add initial state Always add initial state to values loaded from local storage --- hooks/useLocalStorageReducer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hooks/useLocalStorageReducer.ts b/hooks/useLocalStorageReducer.ts index 01d3156c..57146a7a 100644 --- a/hooks/useLocalStorageReducer.ts +++ b/hooks/useLocalStorageReducer.ts @@ -5,7 +5,8 @@ const init = (localStorageKey: string) => (initialState: S) => { const locallyStoredState = localStorage.getItem(localStorageKey) if (locallyStoredState !== null) { console.log(`Found data in ${localStorageKey}`) - return JSON.parse(locallyStoredState) + const state = JSON.parse(locallyStoredState) + return {...initialState, ...state} } else { localStorage.setItem(localStorageKey, JSON.stringify(initialState)) }