Skip to content

The getter method of the makeAutoObservable object returns wrong value. #4566

@davincerepo

Description

@davincerepo

Intended outcome:

Expect the get method to return the field value.

Actual outcome:

It looks like the cached value from the last get call is returned.

How to reproduce the issue:


class TestStore {
    private _source?: string

    constructor() {
        makeAutoObservable(this)
    }

    do(val: any) {
        this.source // To reproduce the problem you must add this line
        this.source = val
        console.log(`TestStore: do, after ${JSON.stringify(this.source)}, after 2 ${JSON.stringify(this._source)}, eq ${this.source === this._source}`);
    }

    get source() {
        return this._source;
    }

    set source(val: string | undefined) {
        this._source = val;
        // Remove this line of log, the do method still has problems
        console.log(`TestStore: set source ${JSON.stringify(val)}, after ${JSON.stringify(this.source)}, after 2 ${JSON.stringify(this._source)}, eq ${this.source === this._source}`);
    }
}

let test = new TestStore()
test.do('123')

Actual log:
LOG TestStore: set source "123", after undefined, after 2 "123", eq false
LOG TestStore: do, after undefined, after 2 "123", eq false

You can see that this._source and this.source return different values, but source is just a simple get method without any other logic. This result is counterintuitive and may be a serious problem.

Printing the log in the setter to access the getter does not affect the result. This is not the root cause of the problem. Even if this line of code is removed, the updated value cannot be obtained in the do method.

Versions
There are problems with both versions.

"mobx": "6.12.3",
"mobx-react-lite": "4.0.7",

"mobx": "6.13.7",
"mobx-react-lite": "4.1.0",

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