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

makeAutoObservable doesn't work with Object.create #3197

Closed
Cerber-Ursi opened this issue Nov 27, 2021 · 1 comment · Fixed by #3198 or #3221
Closed

makeAutoObservable doesn't work with Object.create #3197

Cerber-Ursi opened this issue Nov 27, 2021 · 1 comment · Fixed by #3198 or #3221
Labels

Comments

@Cerber-Ursi
Copy link

Cerber-Ursi commented Nov 27, 2021

I'm trying to make a little library for specific kind of workflow with Mobx. For this, I attempted to use Object.create to place some functions on the prototype of future store, to be used afterwards as actions; according to the documentation, this should be possible:

All functions on prototype become autoAction.

Intended outcome:
Functions defined on prototype via Object.create are converted to actions, changes to observable state made in them is done without warnings.

Actual outcome:
Prototype of the object is ignored, changing observables inside functions leads to a warning.

How to reproduce the issue:

import { makeAutoObservable, reaction } from "mobx";

const store = Object.create({
  inc() {
    this.value += 1;
  }
});
store.value = 0;

makeAutoObservable(store);
reaction(
  () => store.value,
  () => {}
);

store.inc();

Here's the CodeSandbox, which shows a warning in console on startup.


I'm not sure if this is a real error in code. However, the behavior is currently not complying with the documentation, so something probably should be fixed.

@urugator
Copy link
Collaborator

urugator commented Nov 27, 2021

Thanks for the report, should be fixed by #3198.

For the time being, you can workaround it by defining constructor on proto:

const store = Object.create({
  constructor() {},
  inc() {
    this.value += 1;
  }
});

urugator added a commit that referenced this issue Dec 15, 2021
@github-actions github-actions bot mentioned this issue Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants