'How to store local storage and regular data at the same time in a reducer ( redux-persist)?
I have store.js
import {configureStore, combineReducers} from "@reduxjs/toolkit"
import { persistStore, persistReducer, FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import userData from "./userData";
import loadingStatus from "./loadingStatus";
const rootReducer = combineReducers({
data: userData,
});
const persistConfig = {
key: 'root',
storage,
}
const persistedReducer = persistReducer(persistConfig, rootReducer);
const store = configureStore({
reducer: persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
})
export const persistor = persistStore(store);
export default store;
And some slicers :
loadingStatus.js
import {createSlice} from "@reduxjs/toolkit";
const loadingStatus = createSlice({
name: 'loaded',
initialState: {
loaded : false
},
reducers: {
setLoading(state, action) {
return { loaded: action.payload}
}
},
});
export const {setLoading} = loadingStatus.actions;
export default loadingStatus.reducer;
userData.js
import {createSlice} from "@reduxjs/toolkit";
const userData = createSlice({
name: 'data',
initialState: {
data : []
},
reducers: {
getData(state, action) {
return { data: action.payload}
}
},
});
export const {getData} = userData.actions;
export default userData.reducer;
The problem is that I only need to store one state in localStorage - userData (this works now), but I also have a loaded state that I don't need in local storage. How can I store local storage and deffault states at the same time?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|