redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

"Unexpected key" error when attempting to deprecate a redux key

Open sunweiyang opened this issue 6 years ago • 3 comments

I have a React Native app using the latest redux-persist V5. For the next version of my app, I'd like to deprecate some redux keys that are now unnecessary. However, this results in an "Unexpected key" RN error (see screenshot).

If possible, what is the proper way to deprecate redux keys with redux-persist?

And thanks @rt2zz -- redux-persist is fantastic!

img_0078

sunweiyang avatar Dec 13 '17 01:12 sunweiyang

@sunweiyang I'm sure you figured this out by now but one way to "deprecate keys" is by adding the key to the blacklist:

const persistConfig = {
  key: 'root',
  whitelist: ['foo', 'bar'],
  blacklist: ['biz']
}

That will prevent redux-persist from trying to rehydrate the key. Of course, if you're developing locally and get the error, you can just blacklist, reload the app, then remove the blacklist entry and the error will stop showing up.

SaltedBlowfish avatar Feb 10 '18 18:02 SaltedBlowfish

@SaltedBlowfish @sunweiyang that works, or the second more formal but slightly more involved method is to add a migration. You can explicitly delete keys in the migration.

That said this is a common enough use case that I could entertain the idea that the state reconciler should handle this automatically as proposed in this PR: https://github.com/rt2zz/redux-persist/pull/621 - my hesitation there is that it is an opinionated, breaking change and I am not sure how to communicate it to users.

One idea I have thought about is to have a configurable stateReconciler:

stateReconciler: createStateReconciler({ level: 2, auto: true, knownKeys: true })

or something like that.

rt2zz avatar Feb 10 '18 20:02 rt2zz

How does one delete keys in a migration? @rt2zz I don't see any indication for how to do so in the example. Key: undefined does not have this effect, nor did I expect it to.

Edit: Using delete stateCopy['key'] works. Please let me know if this is the recommended solution

kevinEsherick avatar Sep 09 '18 18:09 kevinEsherick