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

unexpected makeAutoObservable overrides #2832

Closed
sheyunsi opened this issue Feb 28, 2021 · 2 comments · Fixed by #2837 or giantswarm/confetti-frontend#158
Closed

unexpected makeAutoObservable overrides #2832

sheyunsi opened this issue Feb 28, 2021 · 2 comments · Fixed by #2837 or giantswarm/confetti-frontend#158
Labels
🐛 bug has PR 🎁 mobx Issue or PR related to mobx package

Comments

@sheyunsi
Copy link

Intended outcome:

import { autorun, makeAutoObservable, observable } from "mobx";

export class ABC {
  constructor() {
    makeAutoObservable(this, {
      data: observable.ref
    });
    autorun(() => {
      if (!this.data) return;
      console.log("DATA CHANGED ", this.data);
    });
  }

  data = null;

  setData = (key) => {
    this.data = { k: "key", v: "value" };
    console.log(`${key}: SET DATA!`);
  };
}

setTimeout(() => {
  const abc1 = new ABC();
  abc1.setData("abc1");
}, 1000);

setTimeout(() => {
  const abc2 = new ABC();
  abc2.setData("abc2");
}, 2000);

output is expected:

abc1: SET DATA! 
DATA CHANGED   {k: "key", v: "value"}

abc2: SET DATA! 
DATA CHANGED   {k: "key", v: "value"}

Actual outcome:

abc1: SET DATA! 
DATA CHANGED   {k: "key", v: "value"}

abc2: SET DATA! 
// abc2.data is not observabled
// makeObservable(this, { data: observable.ref,  setData: action}) it's work

How to reproduce the issue:
https://codesandbox.io/s/withered-star-pmk0z?file=/src/index.js

Versions

mobx: 6.17
@iChenLei iChenLei added the 🎁 mobx Issue or PR related to mobx package label Feb 28, 2021
@akphi
Copy link

akphi commented Mar 1, 2021

@sheyunsi Thank you so much! I was stuck for hours with this and don't even know where to debug, then I searched for makeAutoObservable observable.ref and found your issue 😭

Really weird bug so thank you so much for the fix @iChenLei! Do you know if we can get this out as a patch ASAP when your change is approved/merged? I'm kinda caught in the middle of my upgrade to mobx@6.1.x.

@iChenLei
Copy link
Member

iChenLei commented Mar 1, 2021

I will mentioned this issue in mobx team gitter channel, hope it can be fixed as soon as possible and release a new version.

EDIT: see #2836

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug has PR 🎁 mobx Issue or PR related to mobx package
Projects
None yet
3 participants