Direct Answer
MobX always recomputes a computed value on the next access if any of its dependencies have changed, even after all reactions that read it have been disposed. The computed’s cache is invalidated solely by changes in the dependency graph, not by the number of active observers.
How MobX Handles Computed Lifecycle
A computed value becomes lazy when it has no active observers. In this state:
- Dependency tracking stops to save work.
- When a dependency changes, the computed is marked stale.
- On the next read or when a new observer attaches, MobX checks the stale flag and recomputes if necessary.
Answering the Specific Questions
1. Will MobX recompute after disposal?
Yes. If the underlying observable changed after the last observer was disposed, the cached value is considered stale and will be recomputed on the next access.
2. Does keeping a reference prevent cache invalidation?
No. Holding a reference to the computed object only stops the object itself from being garbage‑collected; it does not influence the stale‑check logic.
3. Do developers need to manually dispose computeds?
Generally no. MobX automatically clears the cache when dependencies change, so manual disposal is only needed if you want to stop a computed from tracking dependencies immediately.
Diagnostic Question
To confirm the behavior in your code, try this minimal test:
import { makeAutoObservable, computed, reaction } from "mobx";
class Store {
count = 0;
constructor() { makeAutoObservable(this); }
}
const store = new Store();
const doubled = computed(() => store.count * 2);
const disposer = reaction(
() => doubled.get(),
val => console.log("doubled:", val)
);
// Dispose the reaction
disposer();
// Update the observable after disposal
store.count = 5;
console.log("after change, doubled:", doubled.get()); // should log 10
If the final log shows the updated value, MobX recomputed the cached value correctly.