Mobx computed example
Shows mobx's computed value feature and how it caches it's value author(s): Matt Ruby
by 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.js"></script>
<script src="https://npmcdn.com/[email protected]"></script>
<script src="https://rawgit.com/mattruby/mobx-examples/master/logging-utils.js"></script>
<script>
wrapConsole();
</script>
JavaScript
console.clear();
var person = mobx.observable({
firstName: 'Matt',
lastName: 'Ruby',
age: 0,
fullName: function () {
console.count('fullName');
return this.firstName + ' ' + this.lastName;
}
});
mobx.autorun(function () {
console.log(person.fullName + ' ' + person.age);
});
// this will print Matt NN 10 times
_.times(10, function () {
person.age = _.random(40);
});
person.firstName = 'Mike';
person.firstName = 'Lissy';