Redux-persist: Dynamic key prefix

Created on 22 Dec 2017  路  10Comments  路  Source: rt2zz/redux-persist

So I've got this app and I've got multiple versions with different app identifiers. I can also have different users logged into the app, and they'll each have different state.

Right now, the key generator looks like this:

const storageKey = ${config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX}${config.key}

I'm thinking that maybe if I store my logged in userid in AsyncStorage and interrogate it directly, I can compose the key prefix using that. Kind of like ${appId}:${userId}. That would give me the separation I want.

But let's consider the use case when a user logs in initially. The config is set without a user id, and then it persists like that. When they log in again, it can't find it because now they've got a user id and their stored state never got saved for them.

What I'd like to float is allowing the key prefix to be either a string or a function. If it's a function, just invoke it when the time comes to persist. I think this would also require that the key itself be calculated when the persistoid is invoked, rather than being hard set when it is instantiated.

Does this seem useful, or am I off base (and maybe this isn't an issue at all)? I'm vetting redux-persist right now.

Most helpful comment

Then why not store all users' data in one place by ID of current user?

{
  "users": {
    "": { "color": "red" },
    "1": { "color": "green" },
    "2": { "color": "blue" }
  }
}

All 10 comments

I'm facing the EXACT same issue, i'm also looking for an answer... @rt2zz ? Any idea ?

Best regards

@rt2zz @vaukalak

Can we discuss about the API for that next to #320 ?

I have similar situation but when thinking about it, I think the solution is to persist user specific state on the server:

  • read state from API when user logs in
  • update state using API on the go or when user logs out
  • at most keep the username of last logged user for the login form

because usually there is only one user currently logged in and it doesn't seem right to keep state of other users in frontend app

@piotr-cz This is not a solution because sometimes you're not online, you could also store offline data that serve as online cache purpose, there is no way to achieve that using backend.

Then why not store all users' data in one place by ID of current user?

{
  "users": {
    "": { "color": "red" },
    "1": { "color": "green" },
    "2": { "color": "blue" }
  }
}

It's a solution yes @piotr-cz, the thing is that my architecture was not thought such that way and i would like to avoid the heavy refactor... :p

@rt2zz Sorry for the poke but I think it's a real problem for a large number of apps that share sessions with user data

@demoran23 or @ScreamZ did you guys ever find a way around this?

We just reset the cache using resettable reducers --' That's kind of workaround

We just reset the cache using resettable reducers --' That's kind of workaround

How is that could you provide an example?

Was this page helpful?
0 / 5 - 0 ratings