【问题标题】:App crashes when updating from app store or play store because it has old localstorage从应用商店或 Play 商店更新时应用崩溃,因为它有旧的本地存储
【发布时间】:2019-09-12 14:37:08
【问题描述】:

我使用 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;
}

【问题讨论】:

  • 在运行此代码之前,如果您怀疑应用程序已更新,为什么不清除本地存储?也许在未来的版本中,您应该存储最后一个已知的版本号并使用它进行比较。
  • 数据库(如 Room)使用版本号和迁移来处理这个问题。 redux-persist-migrate 就是一个例子。

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


【解决方案1】:

如果您正在更改减速器结构,则应该迁移,或者按照 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 })
  };

【讨论】:

  • 谢谢你!你让我的一天,就像从前一天把我的头撞到墙上一样。现在已经解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
相关资源
最近更新 更多