我正在开发一个React Native应用程序.我有一个使用persitCombineReducers设置的reducers组合.
其中一个切片进行了 retrofit ,现在有了新的 struct .
这是旧 struct :
interface OldAuthState {
token?: string;
expiresAt?: string;
customer?: CustomerAuthFragment;
numberOfTimeAskedForData?: number;
lastDateAskedForData?: Date;
dismissedQuizQuestions: string[];
}
这是我想要迁移到的新 struct :
interface AuthToken {
token: string;
expiresAt: string;
}
interface DataInfo {
numberOfTimeAskedForData: number;
lastDateAskedForData?: Date;
}
export interface AuthState {
accessToken?: AuthToken;
customer?: CustomerAuthFragment;
dataInfo: DataInfo;
dismissedQuizQuestions: string[];
}
经过一些在线研究,我发现使用redux—persistent中的迁移和review Migrate函数是可行的.
多亏了副驾驶,我终于有了这个功能
const migrations = {
1: (state: PersistedState) => {
// Extract the old auth state.
const oldAuthState = (state as any).auth as OldAuthState;
// Initialize the new auth state based on the old one.
const newAuthState: AuthState = {
dataInfo: {
numberOfTimeAskedForData: oldAuthState.numberOfTimeAskedForData ?? 0,
lastDateAskedForData: oldAuthState.lastDateAskedForData,
},
accessToken: {
token: oldAuthState.token ?? '',
expiresAt: oldAuthState.expiresAt ?? '',
},
customer: oldAuthState.customer,
dismissedQuizQuestions: oldAuthState.dismissedQuizQuestions,
};
// Return the new state.
return {
...state,
// Replace the old auth state with the new one.
auth: newAuthState,
};
},
};
const migrate = createMigrate(migrations, { debug: process.env.NODE_ENV === 'development' });
我知道副驾驶给了我一些简单的代码来做这件事.另外,我不确定几点:
- 它如何知道在迁移期间提供了哪个持久化状态?
- 如何将以前的国家 struct 与新的国家 struct 相对应?
在网上,我只找到了一些示例,说明如何向状态 struct 添加新值,而不是更新整个状态.另外,我在redux网站上找不到任何关于这个的文档.