So lately I’ve started seeing more and more people use MobX, and I wanted to share some insights I gained while using it for a while. I’m going to write about a few topics that are not that obvious for new MobX users. This article does require basic knowledge or previous usage of Mobx, I’ve prepared Github repo to play with this stuff(here)

Computed Values Caching

So we have this amazing ability called “computed” in MobX which allows deriving values from the state but if you tried to debug it, you will see some of the values are being cached and some are not, so why is that happening? Computed functions are being cached/memoized only when they are being **observed **e.g is watched by autorun, reaction, or mobx-react observer. This is designed like that on purpose to avoid computations all the time and only when there is a change detected by mobx observing it.

More info about it here:

Reassigning Observables

MobX wraps **observables **with its own interface and methods to be able to observe the changes, so if you reassign a value to it, you can possibly remove MobX wrapper and then lose all of its abilities and it will take a while to understand why it happened.

#mobx-react #javascript #mobx #react #engineering

Mobx Tips And Pitfalls
3.75 GEEK