Autostart MobX

I am learning MobX and am intrigued by the problem:

If I have this observable:

class ItemsStore {
    @observable items = [1,2,3];
}
const store = new ItemsStore;

and then change it like this:

setInterval(() => {
    store.items[0] = +new Date
}, 1000)

I noticed the following:

What is the logic behind the API for this? I would expect that since it store.itemsnever works, these immutable properties would behave the same.

And how does MobX know what code is inside my callback? he parses my callback, am I going to autorun?

+4
source share
1 answer

console.log (store.items)

, , , . store.items . store.items.slice() store.items.toJS(), .

console.log(store.items [0])

, , , .

console.log(store.items.length)

, MobX . length :

Object.defineProperty(ObservableArray.prototype, "length", {
    enumerable: false,
    configurable: true,
    get: function(): number {
        return this.$mobx.getArrayLength();
    },
    set: function(newLength: number) {
        this.$mobx.setArrayLength(newLength);
    }
});

getArrayLength , MobX :

getArrayLength(): number {
    this.atom.reportObserved();
    return this.values.length;
}
+1

Source: https://habr.com/ru/post/1663449/


All Articles