javascript - 从应用商店或 Play 商店更新时应用程序崩溃,因为它具有旧的本地存储

标签 javascript reactjs react-native local-storage redux-persist

我使用 redux-persist 将数据保存到 redux 存储中。我现在在商店中添加了一些新标志,但是当从商店更新应用程序时,它不会获得新标志,因为它具有旧的本地存储,并且本地存储在更新应用程序时不会清除。这会导致应用程序崩溃,直到我删除应用程序后重新安装该应用程序为止。

'use strict';

/* React Native */
import { AsyncStorage } from 'react-native';

/* Officetrax */
import { createStore, applyMiddleware } from 'redux';
import app from './reducers';

/* Thunk */
import thunk from 'redux-thunk';

/* Redux Storage */
import excludeSaveActionConstants from './constants/excludeSaveActionConstants';

/* Remote Redux Dev Tools */
import { composeWithDevTools } from 'remote-redux-devtools';

/* Redux Offline */
import { offline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';

/* Redux Logger */
import { createLogger } from 'redux-logger';

export default function configureStore() {




    // Create redux logger
    const logger = createLogger({
        //logger: remoteConsole,
        logErrors: true,
    });

    let persistOptions = { ...offlineConfig, whitelist: excludeSaveActionConstants };

    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || composeWithDevTools;
    // Create the store with middleware applied
    let store = createStore(app, composeEnhancers(
        applyMiddleware(thunk),
        offline(persistOptions)
    ));

    return store;
}

最佳答案

如果您要更改 reducer 结构,则应该迁移,或者按照 Jesse Schokker 建议清除所有缓存,但如果您确实不想丢失某些数据,则可以进行迁移。

import { createMigrate } from 'redux-persist';

const migrations = {
    2: state => {
      const { yourReducer } = state;
      const mynewStructure = { ...yourReducer, myNewKey: 'some value' }; 
      state.yourReducer = mynewStructure;
      return state;
      };
    }
  };

  const persistConfig = {
    ...offlineConfig, 
    whitelist: excludeSaveActionConstants
    version: 2, // Add a version which will correspond to the number declared in your migrate
    migrate: createMigrate(migrations, { debug: false })
  };

关于javascript - 从应用商店或 Play 商店更新时应用程序崩溃,因为它具有旧的本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57909096/

相关文章:

javascript - 为什么 mongo 中的新文档有一个对象而不是 ObjectId?

angularjs - Node项目的应用程序结构

reactjs - props 没有传递给 react native 中的另一个组件

javascript - GLTF加载对象拖拽使用三个js

ios - 为什么我无法查询可用的应用内购买

javascript - 在innerHTML命令之后将JQuery函数重新绑定(bind)到输入字段

Javascript `this` 语句不起作用

react-native - 如何在 react native 中删除 react 导航的事件监听器?

React-Native:如何知道渲染何时完成

javascript - 为什么我不能使用 Javascript innerHTML 属性更新 HTML 列表项?