#store
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { persistReducer, persistStore } from "redux-persist";
import AsyncStorage from '@react-native-async-storage/async-storage';
import addUserSlice, { signInUser } from "./slices/addUserSlice";
import { thunk } from "redux-thunk";
const persistConfig = {
key: 'root',
storage: AsyncStorage,
whitelist:['addUser']
}
const persistedReducer = persistReducer(persistConfig,addUserSlice)
export const store = configureStore(
{
reducer: persistedReducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware({
immutableCheck: false,
serializableCheck: false,
})
}
);
export const persistor = persistStore(store)
#slice
import { createSlice } from "@reduxjs/toolkit";
export const addUserSlice = createSlice({
name:'addUser',
initialState:{
userData:[]
},
reducers:{
signupUser:(state, action)=>{
console.log("adduser slice-----", action.payload)
state.userData.push(action.payload)
},
signInUser:(state, action)=>{
const {username, password} = action.payload
return state.filter(item=>item.username===username && item.password ===password)
}
}
})
export const { signupUser, signInUser } = addUserSlice.actions;
export default addUserSlice.reducer
i was trying to store data in AsyncStorage using Redux-Persist and redux Toolkit. But no data is storing in async stoarge. i need to store the data in async storage so, it should not loss if refresh
3
Answers
#slice
You need to wrap persistor and store into app.js file like :
The
whitelist
config only work one level deep. If you want to persist theaddUser
state slice, you should create a persisted reducer on the root reducer. You can usecombineReducers
function to create the root reducer.store.ts
:Persisted state in local storage:
stackblitz