Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当用户已经有本地持久化存储时,如何重构/重命名Redux状态形状?_Redux - Fatal编程技术网

当用户已经有本地持久化存储时,如何重构/重命名Redux状态形状?

当用户已经有本地持久化存储时,如何重构/重命名Redux状态形状?,redux,Redux,我有一个React本地移动应用程序,它使用Redux和 假设状态形状当前如下所示: notifications: { new_messages: 2, new_friend_requests: 0, new_jobs: 0 } notifications: { new_messages_count: 2, new_friend_requests_count: 0 } 假设我们当前使用1个reducer设置这些值,其中action.notificatio

我有一个React本地移动应用程序,它使用Redux和

假设状态形状当前如下所示:

notifications: {
    new_messages: 2,
    new_friend_requests: 0,
    new_jobs: 0
}
notifications: {
    new_messages_count: 2,
    new_friend_requests_count: 0
}
假设我们当前使用1个reducer设置这些值,其中
action.notifications
是一个字典,其中包含键
new\u messages
new\u friend\u requests
new\u jobs

const notifications = (state = {}, action) => {
  switch (action.type) {
    case SET_NOTIFICATIONS: 
      return action.notifications;
    default:
      return state;
  }
}
我想

  • 摆脱新的工作,因为它不再相关
  • 减速器
  • new\u friend\u requests
    重命名为
    new\u friend\u requests\u count
  • new\u messages
    重命名为
    new\u messages\u count
  • 新的状态形状如下所示:

    notifications: {
        new_messages: 2,
        new_friend_requests: 0,
        new_jobs: 0
    }
    
    notifications: {
        new_messages_count: 2,
        new_friend_requests_count: 0
    }
    
    我们会有新的减速器

    const new_messages_count = (state = {}, action) => {
      switch (action.type) {
        case SET_NEW_MESSAGES: 
          return action.new_messages_count;
        default:
          return state;
      }
    }
    
    const new_friend_requests_count = (state = {}, action) => {
      switch (action.type) {
        case SET_CONNECTION_REQUESTS: 
          return action.new_friend_requests_count;
        default:
          return state;
      }
    }
    
    const notifications = combineReducers({
      new_messages_count,
      new_friend_requests_count
    });
    
    但是,当我进行更改时,会遇到如下错误消息:

    notifications: {
        new_messages: 2,
        new_friend_requests: 0,
        new_jobs: 0
    }
    
    notifications: {
        new_messages_count: 2,
        new_friend_requests_count: 0
    }
    
    还原程序在以前接收到的状态中发现意外键“新建消息”、“新建好友请求”、“新建作业”。希望找到一个已知的reducer键:“new\u messages\u count”、“new\u friend\u requests\u count”。意外的键将被忽略

    我理解为什么会发生这种情况:用户先前持久化的redux状态包含这些键,这些键被重新水化为我的新redux状态形状。我的问题是,处理这种情况的优雅方式是什么


    编辑:我意识到错误消息可能完全是因为重命名了变量,而reducer拆分/分解可能与此无关。尽管如此,您如何才能优雅地重命名这些变量而不发生错误?

    似乎其他人以前也遇到过这种情况,他们的方法是使用版本号,如果本地版本号<当前版本号,则清除状态