I have the following:
import AsyncStorage from '@react-native-async-storage/async-storage'
import { persistStore, persistReducer } from 'redux-persist';
import { configureStore } from "@reduxjs/toolkit";
import { searchReducer } from "./search/searchSlice"
import { userReducer } from "./user/userSlice"
export const store = configureStore({
reducer: {
search: searchReducer,
user: userReducer
}
})
export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
I need to persist only the user
reducer. How can I do it since there isn’t any definition of a ‘user’ reducer outside the configureStore
function? What should be in the place in the "???"
below?
const persistedReducer = persistReducer({
key: "root",
storage: AsyncStorage,
whitelist: [???]
}, ???)
export const persistor = persistStore(store)
2
Answers
You can do as following.
The
persistReducer
function takes two arguments, a persist configuration object and a reducer to persist.The reducer you would like to persist needs to be created and passed to the store when it is created. You can’t persist specific reducers after the fact.
Example:
You can also persist just the
userReducer
if you like. See Nested Persists.Example: