redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Change from AsyncStorage to react-native-mmkv-storage

Open zpinto123 opened this issue 3 years ago • 13 comments

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?

zpinto123 avatar Aug 10 '22 14:08 zpinto123

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?

CiprianDraghici avatar Aug 12 '22 09:08 CiprianDraghici

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

Dragollla avatar Sep 26 '22 20:09 Dragollla

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?

tomgreco avatar Jun 20 '23 14:06 tomgreco

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
  },
}


Dragollla avatar Jun 21 '23 21:06 Dragollla

Hi @Dragollla Where is the location i want to put this code? can you explain little bit ?

ThushalIntervest avatar Dec 06 '23 09:12 ThushalIntervest

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 avatar Dec 06 '23 11:12 Dragollla

@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.

ThushalIntervest avatar Dec 06 '23 12:12 ThushalIntervest

That value is set in persist config, so it should be ok.

  1. getStoredState gives you a slice of state for the key in old storage.
  2. This state is returned from migration.
  3. Then redux-persist will store that state using a key from persist config and new storage.

Dragollla avatar Dec 06 '23 12:12 Dragollla

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?

ThushalIntervest avatar Dec 06 '23 13:12 ThushalIntervest

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.

Dragollla avatar Dec 07 '23 21:12 Dragollla