question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I persist a subset of slices?

See original GitHub issue

Code

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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
pavi2410commented, Mar 4, 2022

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.

1reaction
AnatoleLucetcommented, Nov 30, 2021

I’m pretty sure this is because you’re renaming the field user to auth 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?

partialize: (state) => ({ user: state.user }),
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can i persist only a subset of state from React Redux?
You can simplify your code by passing only the reducer cartReducer . const persistConfig = { key: 'root', ...
Read more >
Developers - Multiple persist slices under one store -
Is there anyway to have multiple slices use different persist storage types? In my use case, I'm trying to have one subset use...
Read more >
How To Persist State In All Slices Of Reducer Using Redux ...
How Do We Use Redux Persist? A. NPM. The EndpointSlice controller should probably have some support for subsetting If we set a limit...
Read more >
Spark - How to slice an array and get a subset of elements
Spark SQL provides a slice() function to get the subset or range of elements from an array (subarray) column of DataFrame and slice...
Read more >
2. Salient Features
However, the application can target the query only to a subset of slices by setting hint on javax.persistence.Query . The hint key is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found