JavaScript

JavaScript: Upgrading from MobX 2.6.0 to 2.7.0

As usual, you should check out the CHANGELOG. However, let me point out a few things specifically:

If you have something like:

observable({
    @computed get someProp() { ... }
});

your code will work in 2.6.0, but in 2.7.0 reactions won’t occur properly. Your tests will end up timing out. It needs to be:

observable({
    get someProp() { ... }
});

Note, there’s no deprecation warning or anything. Also note that this doesn’t apply to situations where you’re using a class. Those will continue working as before.

As of 2.6.2, toJS no longer recurses once it hits something that isn’t observable. Hence, if you have an observable object with a non-observable array with observable objects in it, MobX will only convert the outer object “to JS”. You’ll need to call toJS again for the things inside the non-observable array. This can cause painful to debug errors.

This change was announced for 2.6.0, but it actually happened in 2.6.2 which means that upgrading from 2.6.0 to 2.6.2 isn’t actually as safe as it might appear.

You should also pay careful attention to the section “Automatic inference of computed properties has been deprecated.” However, that’s at least well-documented and results in deprecation warnings. There were a bunch of places where I had code that was passing in a callback. The callback took one parameter. However, in my tests, the callbacks were fake. Hence, they didn’t need that parameter. Our ESLint rules say that you should leave out the parameter in that case since it’s unused. However, once I left out the parameter, MobX kept trying to change the function into a cached, computed getter. The solution was to leave the parameter in place and write the following above each such instance:

// See: https://github.com/mobxjs/mobx/issues/421
// eslint-disable-next-line no-unused-vars

I also added asReference to a few such places when it made sense.

Published on Web Code Geeks with permission by Shannon Behrens, partner at our WCG program. See the original article here: JavaScript: Upgrading from MobX 2.6.0 to 2.7.0

Opinions expressed by Web Code Geeks contributors are their own.

Shannon Behrens

Catholic, father of eight, open source enthusiast, Python, JavaScript, Ruby, Scala, Go, etc. programmer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button