【问题标题】:Mutate redux persist state on development变异 redux 在开发中保持状态
【发布时间】:2019-07-31 18:28:24
【问题描述】:

我正在使用 redux-persist 将我的 redux 阶段保存在 AsyncStorage 上,以做出本机反应。由于它是一个新应用程序,我不断更改我的 redux 初始状态的结构以适应新功能或修复。但是当我重新加载应用程序时,它会被 redux-persist (再水化)持久化的旧状态覆盖。

如何让 redux-persist 保存新的 initialState 而无需在我的开发环境中编写迁移?

【问题讨论】:

    标签: reactjs react-native redux redux-persist


    【解决方案1】:

    我添加了一个组件函数,用于清除持久状态并立即重新加载应用程序。该函数由仅在开发环境中可用的按钮调用。

    import { persistor } from '../../AppContainer' // adjust this line for you
    import RNRestart from 'react-native-restart'
    
    clearPersistedState = () => {
        persistor.purge()
        RNRestart.Restart() // reload the React Native Bundle
    }
    

    有必要立即重新加载应用程序,因为如果在清除持久状态后调用任何 redux 操作,redux-persist 将再次保存旧的 initialState(仍在 RAM 中)。

    【讨论】:

      【解决方案2】:

      如果您尝试适应新功能而不是删除视图所需的内容,则将 automergelevel 设置为 2,因为它只会覆盖现有键,并且您添加的任何新功能都将与您提供的 initialState 一起添加。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        • 2018-12-10
        • 2020-11-17
        • 2022-01-15
        • 2020-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多