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

_reactNativeMmkv.MMKV.set is not a function #61

Closed
shrmaky opened this issue May 3, 2021 · 4 comments
Closed

_reactNativeMmkv.MMKV.set is not a function #61

shrmaky opened this issue May 3, 2021 · 4 comments

Comments

@shrmaky
Copy link

shrmaky commented May 3, 2021

Version used - "react-native-mmkv": "1.1.6"

Tried implementing in my project but throwing error, am I doing something wrong

      import AsyncStorage from '@react-native-community/async-storage';
      import { applyMiddleware, compose, createStore } from 'redux';
      import { persistReducer, persistStore } from 'redux-persist';
      import { MMKV } from 'react-native-mmkv';
      import { Storage } from 'redux-persist';
      
      import thunkMiddleware from 'redux-thunk';
      import { appReducer } from './reducer';
      
      const rootReducer = (state: any, action: any) => {
        if (action.type === 'AppState/ClearCacheRdxConst') {
          return appReducer(undefined, action);
        }
        return appReducer(state, action);
      };
      
      const enhancers = [
        applyMiddleware(
          thunkMiddleware,
          // createLogger({
          //   collapsed: true,
          //   predicate: () => __DEV__,
          // }),
        ),
      ];
      
      // @ts-ignore
      const composeEnhancers =
        (__DEV__ &&
          typeof window !== 'undefined' &&
          window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
        compose;
      
      const enhancer = composeEnhancers(...enhancers);
      
      // Unfortunately redux-persist expects Promises,
      // so we have to wrap our sync calls with Promise resolvers/rejecters
      export const storage: Storage = {
        setItem: (key: string, value: string): Promise<boolean> => {
          MMKV.set(key, value);
          return Promise.resolve(true);
        },
        getItem: (key: string): Promise<string> => {
          const value = MMKV.getString(key);
          return Promise.resolve(value);
        },
        removeItem: (key: string): Promise<void> => {
          MMKV.delete(key);
          return Promise.resolve();
        },
      };
      
      const persistConfig = {
        key: 'root',
        storage: storage,
      };
      
      const persistedReducer = persistReducer(persistConfig, rootReducer);
      export const store = createStore(persistedReducer, undefined);
      export const persistor = persistStore(store);

Error:

Screen Shot 2021-05-03 at 12 11 26 PM

However in the android it is not even able to build:

Screen Shot 2021-05-03 at 12 16 00 PM

@mrousavy
Copy link
Owner

mrousavy commented May 3, 2021

For iOS, did you run pod install?

For Android, did you follow the installation guide?

@mrousavy mrousavy closed this as completed Jun 4, 2021
@jimhill
Copy link

jimhill commented Jun 14, 2021

Hi there @mrousavy - I get this exact same error. It occurs only when I enable Debugging in the browser. MMKV comes back as an object but each key such as set, getString comes back undefined. All of us building our app get the same error.

This is what comes back if I log MMKV:

image

@mrousavy
Copy link
Owner

That's because browser debugging doesn't work with JSI. You have to use the Hermes debugger or Safari debugger.

@jimhill
Copy link

jimhill commented Jun 14, 2021

Excellent. Thank you. Makes perfect sense.

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

3 participants