react-native-mmkv: Storage is unavailable

Hello, I use mmkv + zustand, and I have such a flow

  • user enters PIN code
  • I init encrypted store with that PIN
  • I start using useEncryptedStore inside the app with zustand

After a while started seeing such an error in logs [zustand persist middleware] Unable to update item '@encrypted', the given storage is currently unavailable.. I tried to re-install the app, but that didn’t help.

Here is the code

// MMKV.ts

let encryptedStorage: MMKV | undefined;

const initEncryptedStorage = (encryptionKey: string) => {
    encryptedStorage = new MMKV({
        id: ENCRYPTED_ID,
        encryptionKey,
    });
    
    if (__DEV__) {
        initializeMMKVFlipper({ encryptedStorage, regularStorage });
    }
};
//Auth.tsx

const handleEnterPIN = (pinCode: string) => {
      initEncryptedStorage(pinCode);
      navigate(PATH.MAIN_SCREEN);
}
//encryptedStore.ts

export const useStoreEncrypted = create<EncryptedInitialStateModel & EncryptedStoreModel>(
    persist({
      ...
    }, {
            name: ENCRYPTED_ID,
            getStorage: () => mmkvToZustandStorage(encryptedStorage),
        },
    ),
);
//Main.tsx

const user = useStoreEncrypted((state) => state.user);
const setUser = useStoreEncrypted((state) => state.setUser);

// on setUser call I get [zustand persist middleware] Unable to update item '@encrypted', the given storage is currently unavailable.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

thanks, otherwise it’s just impossible for me to fix it