Reputation: 30116
I'm new to mobx and working my way through the docs. After reading about observable
and autorun
I created a small testbed to play with the functionality locally.
When executing the following code in nodejs 13.12 with mobx 5.15.4, the autorun function is only called once. It does not react to the last line, where numbers is updated.
const mobx = require('mobx')
const {observable, autorun} = mobx;
// just to make sure it is imported
console.log(observable);
var numbers = observable([1, 2, 3]);
autorun(() => {
console.log(numbers);
});
numbers.push(4);
This seems to be a problem specific to my local runtime. At first I had phrased the question more generally, but when executed in the browser, the code worked. It is only the specific example above, which fails in nodejs. Many thanks to @felixmosh for pointing out that the problem can not be reproduced in the browser.
Upvotes: 2
Views: 506
Reputation: 35533
Just like the @observer decorator/function, autorun will only observe data that is used during the execution of the provided function.
Actually, I've run your code, and the autorun works as expected.
// example 2, array of primitives
// observes computed value, works
const {
observable,
computed,
autorun
} = mobx;
var numbers = observable([1, 2, 3]);
autorun(() => {
console.log(numbers);
});
numbers.push(4); //autorun does not trigger
<script src="https://cdnjs.cloudflare.com/ajax/libs/mobx/5.15.4/mobx.umd.min.js"></script>
Upvotes: 2