quarrant / mobx-persist-store

Persist and rehydrate observable properties in mobx store.
268 stars 14 forks source link

mobx-react #112

Open Aleksandern opened 9 months ago

Aleksandern commented 9 months ago

I'm not sure if it's related to 'mobx-persist-store', but maybe somebody can advise me. The issue is I can't set the value to 'null' for a field.

import {
  configure,
} from 'mobx';
import {
  configurePersistable,
} from 'mobx-persist-store';
import {
  enableStaticRendering,
} from 'mobx-react';
import localForage from 'localforage';

configure({ enforceActions: 'always' });

configurePersistable(
  {
    storage: localForage,
    expireIn: 86400000,
    removeOnExpiration: true,
    stringify: false,
    debugMode: true,
  },
  {
    delay: 200,
    fireImmediately: false,
  },
);

export class AuthStore {
  @observable
  user: {} | null = null;

  constructor() {
    super();
    makeObservable(this);

     makePersistable(this, {
       'AuthStore',
        ['user'],
      });
  }

  setUser() {
   this.user = {
     info: {
       email: 'email@email.com',
     },
     token: 'string value',
  };

  resetUser() {
     this.user = null;
  }
}

On the login page, I call AuthStore.setUser and on the logout page I call AuthStore.resetUser.

After I call AuthStore.setUser then this.user has the needed value and after I reload the page the needed value is still there. So the persisting is working here.

But after I call AuthStore.resetUser then this.user is null but after I reload the page then 'this.user' is not null but must be null.

That happens only with mobx-react@9 mobx-react@7 works fine.

    "react": "18.2.0",
    "mobx": "^6.10.2",
    "mobx-persist-store": "^1.1.3",
    "mobx-react": "^9.1.0",
    "mobx-utils": "^6.0.8",
Aleksandern commented 9 months ago

Hi @quarrant Do you still maintain this project? Or maybe you know an alternative "mobx persistent" package?

quarrant commented 8 months ago

Hello! I'm not sure this issue about mobx-persist-store if it works well with mobx-react@7 but doesn't work with mobx-react@9. You can try to check localForage after resetUser and see real persisted data.