react-native-mmkv icon indicating copy to clipboard operation
react-native-mmkv copied to clipboard

How to change AsyncStorage with MMKV in redux persist

Open anis-18 opened this issue 2 years ago • 3 comments

I'm trying to migrate my app from AsyncStorage to MMKV. I'm using redux toolkit and redux toolkit. I've found some resources that are using that but I'm not able to understand why they are doing it. They are creating functions set and get with key and value and they're creating a middleware and adding it to redux store. Please can someone explain to me what should I do to accomplish this thing. This my store code. Th app is not yet published, so I don't care about user data. I want to change AsyncStorage with MMKV in my app.

import { configureStore } from '@reduxjs/toolkit'; 
import { combineReducers } from 'redux'; 
import { persistReducer } from 'redux-persist';
 import { profileSlice } from 'features/user/store/profile-slice';
 import thunk from 'redux-thunk'; 
import AsyncStorage from '@react-native-async-storage/async-storage'; 
import { garageSlice } from 'features/garage/store/garage-slice';

 const reducers = combineReducers({
 profile: profileSlice.reducer, 
garage: garageSlice.reducer, 
});
 const persistConfig = { 
     key: 'root', 
     storage: AsyncStorage, 
}; 
let middlewares = [thunk]; 
if (__DEV__) { 
const createDebugger = require('redux-flipper').default; middlewares = [...middlewares, createDebugger()];
 } 
const persistedReducer = persistReducer(persistConfig, reducers); 
export const store = configureStore({ 
reducer: persistedReducer, 
devTools: process.env.NODE_ENV !== 'production', middleware: middlewares, 
});
 export type AppDispatch = typeof store.dispatch; export type RootState = ReturnType<typeof store.getState>;

What are the modifications that I should do to migrate from AsyncStorage to MMKV ? I've found this documentation https://github.com/mrousavy/react-native-mmkv/blob/master/docs/WRAPPER_REDUX.md I understand that I should create the storage object and put it as storage in my redux persist config instead of AsyncStorage. But why should I create all these functions ?

anis-18 avatar Nov 02 '23 05:11 anis-18

Translate it: Se necesita un contrato para que redux pueda saber donde guardar, donde leer y donde eliminar.

interface Storage {
setItem: (key, value) => any
getItem: (key) => any
removeItem: (key) => any
}

Con ese contrato a Redux-persist puede usar cualquier base de datos.

zWilliamTM avatar Dec 26 '23 16:12 zWilliamTM

🤣

mrousavy avatar Dec 27 '23 10:12 mrousavy

Steps for doing so: https://dev.to/ajmal_hasan/react-native-mmkv-5787

Ajmal0197 avatar Feb 06 '24 21:02 Ajmal0197