我想为 redux-persist 编写一个迁移,但不知何故迁移没有被应用。我遵循了有关迁移的官方文档: https://github.com/rt2zz/redux-persist/blob/master/docs/migrations.md
但是下面代码中的某些内容不起作用,因为我的 redux-persist 没有应用迁移。
当我尝试调试此迁移时,它说版本号是相同的。
Redux-persist version match noop migration
不确定我是否在配置或迁移编写中遗漏了某些内容。
在我的商店中,我有类似的持久配置
const persistConfig: PersistConfig = {
key: 'primary',
storage,
blacklist: [
],
stateReconciler: autoMergeLevel2,
version: 1,
migrate: createMigrate(migrations, { debug: true })
} as PersistConfig;
我的迁移看起来像:
const migrations = {
0: (state: StoreState) => {
return {
...state,
application: {
...state.application
}
}
},
1: (state: StoreState) => {
return {
...state,
application: {
...state.application,
items:[{id: 1, isDone: false}, {id: 2, isDone: false}].map((item, index)=>{
if(item.id == 1){
item.isDone = true
}
}),
},
}
}
}
在我的状态和应用程序缩减器的第一个版本中,我的对象项如下所示:
items:[
{id: 1},
{id: 2}
]
现在它需要看起来像:
items:[
{id: 1, isDone: false/true},
{id: 2, isDone: false/true}
]
最佳答案
您需要升级 redux-persist
的 persistConfig
对象中的版本。
如果某个版本已运行一次迁移,则不会再次为同一版本运行。
之前
const persistConfig = {
key: "root",
storage,
version: 0,
migrate: createMigrate(migrations, { debug: true }),
stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
whitelist: ["auth", "user", "app", "card"]
};
之后
const persistConfig = {
key: "root",
storage,
version: 1,
migrate: createMigrate(migrations, { debug: true }),
stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
whitelist: ["auth", "user", "app", "card"]
};
关于reactjs - Redux-persist迁移不迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58269323/