Skip to content

Commit 9613921

Browse files
committed
Update readme with migration snippet
Original issues: rt2zz/redux-persist#199 rt2zz/redux-persist#284 Converastions related to provided snippet: rt2zz/redux-persist#679 robwalkerco#7
1 parent 55325f0 commit 9613921

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,44 @@ persistStore(
5151
},
5252
)
5353
)
54+
```
55+
56+
## migration from previous storage
57+
58+
**tested with redux-persist v4**
5459
60+
the snippet below lets you migrate redux data previously stored in
61+
`AsyncStorage` to `redux-persist-filesystem-storage`.
62+
63+
**NOTE** This snippet lets you migrate healthy data. It will not _restore_
64+
data if it is already hit limits of `AsyncStorage`
65+
66+
```javascript
67+
import { persistStore, getStoredState } from 'redux-persist'
68+
import FilesystemStorage from 'redux-persist-filesystem-storage'
69+
import { AsyncStorage } from 'react-native'
70+
import _ from 'lodash'
71+
import { createStore } from 'redux'
72+
73+
const store = createStore(...)
74+
75+
// create persistor for `redux-persist-filesystem-storage`
76+
const fsPersistor = persistStore(
77+
store,
78+
{ storage: FilesystemStorage },
79+
async (fsError, fsResult) => {
80+
if (_.isEmpty(fsResult)) {
81+
// if state from fs storage is empty try to read state from previous storage
82+
try {
83+
const asyncState = await getStoredState({ storage: AsyncStorage })
84+
if (!_.isEmpty(asyncState)) {
85+
// if data exists in `AsyncStorage` - rehydrate fs persistor with it
86+
fsPersistor.rehydrate(asyncState, { serial: false })
87+
}
88+
} catch (getStateError) {
89+
console.warn("getStoredState error", getStateError)
90+
}
91+
}
92+
}
93+
)
5594
```

0 commit comments

Comments
 (0)