Mobx autorun example
Shows mobx.reaction
author(s):
Matt Ruby
HTML
<div id="react-log"></div>
<script src="https://cdn.jsdelivr.net/g/[email protected](react.min.js+react-dom.min.js),[email protected]"></script>
<script src="https://npmcdn.com/[email protected]/lib/mobx.umd.min.js"></script>
<script src="https://npmcdn.com/mobx-react"></script>
<script src="https://rawgit.com/mattruby/mobx-examples/master/logging-utils.js"></script>
<script>
wrapConsole();
</script>
JavaScript
var person = mobx.observable({
firstName: 'Matt',
lastName: 'Ruby',
age: 0
});
// note that this will not fire initially, only on change.
// so you will not see the age 0 run first unlike autorun.
mobx.reaction(function () {
return person.firstName + ' ' + person.age;
}, function (fnameAndAge) {
console.log(fnameAndAge + ' ' + person.lastName);
});
// this will print Matt NN 10 times
_.times(10, function () {
person.age = _.random(40);
});
// this will print nothing
_.times(10, function () {
person.lastName = _.random(40);
});