parse-community / parse-react

[EXPERIMENTAL] React, React Native, and React with SSR (e.g. Next.js) packages to interact with Parse Server backend
https://parseplatform.org/
MIT License
70 stars 27 forks source link

Add ability to use another local storage instead of AsyncStorage #110

Open flixyudh opened 1 year ago

flixyudh commented 1 year ago

is it possible to use another local storage like RN MMKV-storage instead of AsyncStorage ?

I've tried something like this:

import Parse from 'parse/react-native';
import {MMKV} from './mmkv';

Parse.setAsyncStorage(MMKV); //here's i used MMKV instead of AsyncStorage
Parse.initialize('APP ID', 'JS ID');
Parse.serverURL = 'Server URL';

export const register = async (email, password) => {
  try {
    return await Parse.User.signUp(email, password);
  } catch (error) {
    alert(error);
  }
};

and an error appear

error XMLHttpRequest failed: "this.getAsyncStorage is not a function. (In 'this.getAsyncStorage()', 'this.getAsyncStorage' is undefined)"

dependencies:

"parse": "^3.5.0-beta.1",
"react-native-mmkv-storage": "^0.8.0",

but when i used "parse": "^3.4.4", there's no error and i think Parse class get stuck in

export const register = async (email, password) => {
  try {
    console.log('start') //appear
    return await Parse.User.signUp(email, password); //stuck here
    console.log('end') //never called
  } catch (error) {
    alert(error);
  }
};
mortenmo commented 1 year ago

MMKV isn't async, I've set a new StorageController with MMKV this way:

const storage = new MMKV();
Parse.CoreManager.setStorageController({
    async: 0,

    getItem(path: string): string | null {
        return storage.getString(path) || null;
    },

    setItem(path: string, value: string) {
        storage.set(path, value);
    },

    removeItem(path: string) {
        storage.delete(path);
    },

    getAllKeys() {
        return storage.getAllKeys();
    },

    clear() {
        storage.clearAll();
    },
});

It would be great if that's an option within the library however, not just ASyncStorage