From 96139212d4d5c63a26b8cb7e061adfcf9335cd1c Mon Sep 17 00:00:00 2001 From: Maksym Komarychev Date: Sat, 27 Jan 2018 00:51:12 +0200 Subject: [PATCH] Update readme with migration snippet Original issues: https://github.com/rt2zz/redux-persist/issues/199 https://github.com/rt2zz/redux-persist/issues/284 Converastions related to provided snippet: https://github.com/rt2zz/redux-persist/issues/679 https://github.com/robwalkerco/redux-persist-filesystem-storage/issues/7 --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index f2673fd..f2b07c6 100644 --- a/README.md +++ b/README.md @@ -51,5 +51,44 @@ persistStore( }, ) ) +``` + +## migration from previous storage + +**tested with redux-persist v4** +the snippet below lets you migrate redux data previously stored in +`AsyncStorage` to `redux-persist-filesystem-storage`. + +**NOTE** This snippet lets you migrate healthy data. It will not _restore_ +data if it is already hit limits of `AsyncStorage` + +```javascript +import { persistStore, getStoredState } from 'redux-persist' +import FilesystemStorage from 'redux-persist-filesystem-storage' +import { AsyncStorage } from 'react-native' +import _ from 'lodash' +import { createStore } from 'redux' + +const store = createStore(...) + +// create persistor for `redux-persist-filesystem-storage` +const fsPersistor = persistStore( + store, + { storage: FilesystemStorage }, + async (fsError, fsResult) => { + if (_.isEmpty(fsResult)) { + // if state from fs storage is empty try to read state from previous storage + try { + const asyncState = await getStoredState({ storage: AsyncStorage }) + if (!_.isEmpty(asyncState)) { + // if data exists in `AsyncStorage` - rehydrate fs persistor with it + fsPersistor.rehydrate(asyncState, { serial: false }) + } + } catch (getStateError) { + console.warn("getStoredState error", getStateError) + } + } + } +) ```