Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change AsyncStorage with MMKV in redux persist #596

Open
anis-18 opened this issue Nov 2, 2023 · 3 comments
Open

How to change AsyncStorage with MMKV in redux persist #596

anis-18 opened this issue Nov 2, 2023 · 3 comments

Comments

@anis-18
Copy link

anis-18 commented Nov 2, 2023

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 ?

@zWilliamTM
Copy link

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.

@mrousavy
Copy link
Owner

🤣

@Ajmal0197
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants