redux,版本预载状态?

问题描述:

http://redux.js.org/docs/api/createStore.htmlredux,版本预载状态?

[preloadedState](任意):初始状态。您可以选择指定 它来保护通用应用程序中的服务器状态,或者恢复先前序列化的用户会话。

我可以使用preloadedState参数成功初始化存储。

但是有时我需要改变状态的结构。

举个例子,我最初有

{ 
    totalCount: 3, 
    usedCount: 1 
} 

现在我想将其更改为

{ 
    totalCount: 3, 
    unusedCount: 2 
} 

然后存储的状态在第一结构现在是无效的。
至少我想放弃旧的状态,并从新的初始状态重新开始。

我在服务器中存储状态并将其用作preloadedState参数。
有没有办法在状态结构更改时放弃服务器存储状态?

我分享我的解决方案。 我创建HOC减速器(http://redux.js.org/docs/recipes/reducers/ReusingReducerLogic.html

export function versionedReducer(reducerFunction) { 

    return (state, action) => { 

    const isInitializationCall = state === undefined 

    if (isInitializationCall) { 
     return state 
    } 

    const { version } = reducerFunction(undefined, {type: undefined}) 
    if (!version) { 
     throw "should versioned" 
    } 
    const shouldAcceptPreloadedState = state.version && state.version >= version 

    if (!shouldAcceptPreloadedState) { 
     state = undefined 
    } 

    return reducerFunction(state, action) 

    } 
}