rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.91k stars 862 forks source link

Change from AsyncStorage to react-native-mmkv-storage #1402

Open zpinto123 opened 1 year ago

zpinto123 commented 1 year ago

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?

CiprianDraghici commented 1 year ago

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?

Dragollla commented 1 year ago

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

tomgreco commented 1 year ago

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?

Dragollla commented 1 year ago

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
  },
}
ThushalIntervest commented 7 months ago

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

Dragollla commented 7 months ago

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

ThushalIntervest commented 7 months ago

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

Dragollla commented 7 months ago

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.
ThushalIntervest commented 7 months ago
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?

Dragollla commented 7 months ago

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.

grifotv commented 2 months ago

@ThushalIntervest I was wondering if you managed to do the migration successfully? I'd really appreciate if you could share your working example. Thanks!