redux-persist
redux-persist copied to clipboard
Change from AsyncStorage to react-native-mmkv-storage
Hey,
I want to change the storage engine, from AsyncStorage to react-native-mmkv-storage, but the app is already in production and installed on many user devices.
How would one go about changing it? Do I just replace it in the config or do I have to do some sort of migration, pulling the current state from the AsyncStorage and putting it into react-native-mmkv-storage?
Is there like a guide or something?
Hey, I have a similar scenario where I want to set the storage dynamically (depending on the client's needs), but I didn't find a solution. There is a workaround for this?
In migration you can use getStoredState with previous config and adapter from asyncStorage. Read previous state, then return it from migrate method and it will be persisted with new config and new adapter
In migration you can use getStoredState with previous config and adapter from asyncStorage. Read previous state, then return it from migrate method and it will be persisted with new config and new adapter
Can you share an example?
sure:
import AsyncStorage from '@react-native-async-storage/async-storage'
import { getStoredState } from 'redux-persist'
...
const migrations: MigrationManifest = {
0: ...,
1: state => {
const oldState = await getStoredState({
key,
storage: AsyncStorage,
})
return oldState
},
}
Hi @Dragollla Where is the location i want to put this code? can you explain little bit ?
Hi Please look at this guide: https://github.com/rt2zz/redux-persist/blob/master/docs/migrations.md
You need to reference migration in your persist config
@Dragollla 🙏 Thank you. I just saw the documentation. But one thing need to clarify, when migrating AsyncStorage to MMKV there is a key for redux-persist. Do i need migrate that value(redux states) also or is that value migrating from this method? Again Thank you for the quick reply.
That value is set in persist config, so it should be ok.
- getStoredState gives you a slice of state for the key in old storage.
- This state is returned from migration.
- Then redux-persist will store that state using a key from persist config and new storage.
const migrations = {
1: async(state) => {
const oldState = await getStoredState({
key,
storage: AsyncStorage,
})
return oldState
},
}
const config = {
timeout: 50000,
key: 'key',
storage: reduxStorage,
blacklist: ['lmkReducer'],
migrate: createMigrate(migrations, { debug: true }),
};
I try using this way but it didn't work for me. do you know why is that?
Maybe try to add version in persist config equal to index of your migration (1). And check, that "key" property is equal to 'key' as in config.