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

Persistance not working on objects when adding properties using set method #82

Open
Pustelto opened this issue Oct 18, 2019 · 0 comments

Comments

@Pustelto
Copy link

Hi,

I have store where I store if some section in the UI is collapsed. It is and object indexed by id of the section. When I save these like this (for initial fetch), persistance works:

class UiStore {
  closedSections = {};

  toggleSection = (sectionId: string, newState?: boolean) => {
    if (newState === false) {
      // This works I have to get old sections and resave entire object again
      const oldSections = toJS(this.closedSections) || {};
      this.closedSections = { ...oldSections, [sectionId]: true };
    } else {
      remove(this.closedSections, sectionId);
    }
  };
}

decorate(UiStore, {
  closedSections: [persist('object'), observable],
  isClosed: observable,
  toggleSection: action,
  clearDataAfterLogout: action,
});

export default new UiStore();

but that is too tedious, I would prefer to use set mobx method. But that does not seems to work (see example below).

class UiStore {
  closedSections = {};

  toggleSection = (sectionId: string, newState?: boolean) => {
    if (newState === false) {
      // This does not work after rehdyratation closedSections is empty object
      set(this.closedSections, sectionId, true);
    } else {
      remove(this.closedSections, sectionId);
    }
  };
}

decorate(UiStore, {
  closedSections: [persist('object'), observable],
  isClosed: observable,
  toggleSection: action,
  clearDataAfterLogout: action,
});

export default new UiStore();

Any idea how to do it? Readme is not very clear about this. I also did not found any answer in the issues.

Thanks for help.

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

1 participant