Created
January 6, 2022 09:57
-
-
Save lior-amsalem/c7b2276515c642f4e932a0e6da0c6f24 to your computer and use it in GitHub Desktop.
redux store with localstorage api stitch together
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
// store.js | |
import { configureStore } from "@reduxjs/toolkit"; | |
import scoreBoardSlice from '../ScoreBoard/scoreBoardSlice'; // ignore | |
import { loadState, saveState } from './localStorage'; // <-- our utility of localstorage | |
import { debounce } from './index'; // <-- our utility to prevent writing too often. | |
const persistedState = loadState(); | |
const store = configureStore({ | |
reducer: scoreBoardSlice.reducer, | |
preloadedState: persistedState, // <--- where we pre-load data we saved to local storeage | |
}); | |
store.subscribe(debounce(() => { | |
saveState({ | |
scoreBoard: store.getState().scoreBoard, // <--- where we save data to localstorage once every 1000ms | |
}); | |
}, 1000)); | |
export { | |
store | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment