Redux - Trying to add a function to configureStore
I'm the creator of Redux Toolkit. persistedState
is not a valid configuration option for configureStore
. The correct field name is preloadedState
.
Instead of using configureStore use createStore. For me this works:
const store = createStore(
reducer,
persistedState
);
full example:
const loadFromLocalStorage = () => {
try {
const serializedState = localStorage.getItem('state');
return JSON.parse(serializedState);
} catch (e) {
throw new Error(e)
}
};
const saveToLocalStorage = state => {
try {
const serializedState = JSON.stringify(state);
localStorage.setItem('state', serializedState);
} catch (e) {
throw new Error(e)
}
};
const state= loadFromLocalStorage();
const store = createStore(
reducer,
state
);
store.subscribe(() => {
saveToLocalStorage(store.getState());
});