I am trying to reset everything when the user logout from the app. But I can’t find a way to delete my persist configs.
Here is my implementation
reducers
import { combineReducers } from 'redux';
import { persistReducer } from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';
import createSensitiveStorage from 'redux-persist-sensitive-storage';
import appReducer from '../app';
import authReducer from '../auth';
import notificationsReducer from '../notifications';
import businessReducer from '../businesses';
import pendingPostReducer from '../pendingPost';
import postsReducer from '../posts';
import otherUsersReducer from '../otherUsers';
import searchHistoryReducer from '../searchHistory';
import rentRequestReducer from '../rentRequests';
import visitRequestsReducer from '../visitRequests';
import commonReducer from '../common';
import conversations from '../conversations';
import messages from '../messages';
import reviews from '../reviews';
const sensitiveStorage = createSensitiveStorage({
keychainService: 'myKeychain',
sharedPreferencesName: 'mySharedPrefs',
});
const appPersistConfig = {
key: 'app',
storage: AsyncStorage,
};
const authPersistConfig = {
key: 'auth',
storage: sensitiveStorage,
};
const searchHistoryPersistConfig = {
key: 'searchHistory',
storage: AsyncStorage,
};
const combinedReducer = combineReducers({
app: persistReducer(appPersistConfig, appReducer),
auth: persistReducer(authPersistConfig, authReducer),
searchHistory: persistReducer(
searchHistoryPersistConfig,
searchHistoryReducer
),
otherUsers: otherUsersReducer,
notifications: notificationsReducer,
business: businessReducer,
pendingPost: pendingPostReducer,
rentRequests: rentRequestReducer,
posts: postsReducer,
visitRequests: visitRequestsReducer,
common: commonReducer,
conversations: conversations,
messages: messages,
reviews: reviews,
});
const rootReducer = (state, action) => {
if (action.type === 'auth/logout/fulfilled') {
return combinedReducer(undefined, action);
}
return combinedReducer(state, action);
};
export default rootReducer;
store
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import { composeWithDevTools } from 'remote-redux-devtools';
import { persistStore } from 'redux-persist';
import rootReducer from './reducers';
const loggerMiddleware = createLogger({
predicate: (getstate, actions) => __DEV__,
});
const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(thunk, loggerMiddleware))
);
export let persistedStore = persistStore(store);
export default store;
2
Answers
If the issue that you are passing
undefined
to the root reducer and this is just rehydrating the state then an alternative may be to "overwrite" theauthReducer
auth state so these are persisted.Example:
Assume that you want to purge all cached data when user logged out.