Created
April 8, 2020 08:43
-
-
Save btodts/c886be68b4bc6fbe47cd097dbcd3bda0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore } from "redux"; | |
import rootReducer from "./reducers"; | |
const localStorageKey = "theme"; | |
const persistedTheme = localStorage.getItem(localStorageKey); | |
let initialState = { | |
preferences: persistedTheme ? JSON.parse(persistedTheme) : {}, | |
}; | |
const store = createStore(rootReducer, initialState); | |
store.subscribe(() => { | |
const preferences = store.getState().preferences; | |
if (!preferences) return; | |
localStorage.setItem(localStorageKey, JSON.stringify(preferences)); | |
}); | |
export default store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment