mobxmobx-reactmobx-react-lite

Why mobx computed getter doesn't call after change value


I have TestStore and observable property fields.

When I click to any name, i call action changeOneName and change some object inside fields. Computed getter hasError called again and i see console.log("hasError computed");

Why don't I see console.log("valueFields computed"); second time after changing name to 'ErrorName'?

https://codesandbox.io/s/vibrant-lumiere-cv2tp?file=/src/TestStore.js


Solution

  • You only changed name property of the object, not the object itself. And Object.values only dereferences direct values (objects) of your fields object, not the inner things, like name. So computed don't need to rerun because things that were referenced in that computed didn't change.

    hasError did rerun because you actually dereference name property inside of it, so when name changes it reruns.

    Hope it makes sense.