How do I persist a subset of slices?
See original GitHub issueCode
export const useStore = create<StoreState>(persist(
// Define the slices that we want to persist across page loads.
(set, get) => ({
...createAuthSlice(set, get), // <-- to be stored in session
...createUserSlice(set, get),
...createPreferenceSlice(set, get),
}),
{
// Configure persist middleware
name: "store", // Name of persistent storage
getStorage: () => sessionStorage, // Use the browser's session storage
partialize: (state) => ({ auth: state.user }),
}
))
I am trying to persist only the user attribute in the auth slice in session, but I keep getting the following error.
Type '(state: { user: IUser; getAccessToken: any; forgotPassword: any; forgotPasswordSubmit: any; signUp: any; signIn: any; logOut: any; verifyUser: any; }) => { auth: IUser; }' is not assignable to type '(state: { user: IUser; getAccessToken: any; forgotPassword: any; forgotPasswordSubmit: any; signUp: any; signIn: any; logOut: any; verifyUser: any; }) => DeepPartial<{ user: IUser; getAccessToken: any; ... 5 more ...; verifyUser: any; }>'. Type '{ auth: IUser; }' has no properties in common with type 'DeepPartial<{ user: IUser; getAccessToken: any; forgotPassword: any; forgotPasswordSubmit: any; signUp: any; signIn: any; logOut: any; verifyUser: any; }>'.ts(2322) middleware.d.ts(135, 5): The expected type comes from property 'partialize' which is declared here on type 'PersistOptions<{ user: IUser; getAccessToken: any; forgotPassword: any; forgotPasswordSubmit: any; signUp: any; signIn: any; logOut: any; verifyUser: any; }, Partial<{ user: IUser; getAccessToken: any; ... 5 more ...; verifyUser: any; }>>' (property) partialize?: ((state: AuthSlice) => DeepPartial<AuthSlice>) | undefined
I would appreciate any help anyone can give me.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5
I don’t think partialize is the right solution to this, as we couldn’t set a different storage for different slices. The approach presented in #800 could be a good solution.
I’m pretty sure this is because you’re renaming the field
user
toauth
in partialize. Partialize is not intended for renaming fields. Only for “partializing” the state object while keeping the same structure.Could you try the following?