Mobx autorun example

Shows mobx.autorun and how it only updates when a field it's using is changed author(s): Matt Ruby

by Matt Ruby

HTML

<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>
Open your console. Then your ]v[ind.

JavaScript

console.clear();

var person = mobx.observable({
  firstName: 'Matt',
  lastName: 'Ruby',
  age: 0
});

mobx.autorun(function() {
  console.log(person.firstName + ' ' + person.age);
});

// 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);
});