Skip to content

Change in behaviour in mobx v5 and v6 #4569

@samcooke98

Description

@samcooke98

Intended outcome:

In MobX v5, the following code would cause a crash in both the production and development build variants.

In MobX v6, this is no longer the case.

// v5 
import * as mobx from "mobx/lib/mobx.min.js";
// v6
// import * as mobx from "mobx/dist/mobx.cjs.production.min.js";


mobx.configure({ enforceActions: "observed" });

const store = mobx.observable({ count: 0 });
mobx.reaction(
  () => store.count,
  (val) => console.log("Store is: " + val)
);

mobx.runInAction(() => {
  store.count++;
});
// works;

store.count++;
console.log(store.count);

Codesandbox (v6): https://codesandbox.io/p/sandbox/zealous-volhard-zfm5tj
Codesandbox (v5): https://codesandbox.io/p/devbox/stupefied-fog-527m2r?workspaceId=ws_uqh2WMCajD26pqSUXVFEd

While breaking-changes are to be expected between major versions, I can't seem to find any GH issue, notes in the changelog, or PRs, that explain if this behaviour was or wasn't intentional.

The only reference I can find is #1758 which suggests to leave it up to consumers of the library to decide.

Actual outcome:

I expect that, if strict mode is enabled, for an error to be thrown regardless of the minified vs development

v5(development):

Image

V5 (production):

Image

V6 (production):

Image

V6 (development):

Image

NB: Is it a bug that the value is modified despite the error message implying it's not?

How to reproduce the issue:

Codesandbox (v6): https://codesandbox.io/p/sandbox/zealous-volhard-zfm5tj
Codesandbox (v5): https://codesandbox.io/p/devbox/stupefied-fog-527m2r?workspaceId=ws_uqh2WMCajD26pqSUXVFEd


I'm willing to open a PR for this. I understand that changing this behaviour back to v5-behaviour is likely to need to be done in a major release.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions