keepAlive
MobX normally suspends any computed value that is not in use by any reaction, and lazily re-evaluates the expression, if needed outside a reaction while not in use. keepAlive
marks a computed value as always in use, meaning that it will always fresh, but never disposed automatically.
Parameters
_1
_2
computedValue
(IComputedValue<any>1
): created using thecomputed
function
Or,
_1
_2
target
(Object
): an object that has a computed property, created by@computed
orextendObservable
property
(String
): the name of the property to keep alive
Returns
IDisposer
: stops this keep alive so that the computed value goes back to normal behavior
Examples
const number = observable(3)
const doubler = computed(() => number.get() * 2)
const stop = keepAlive(doubler)
// doubler will now stay in sync reactively even when there are no further observers
stop()
// normal behavior, doubler results will be recomputed if not observed but needed, but lazily
const obj = observable({
number: 3,
doubler: function() { return this.number * 2 }
})
const stop = keepAlive(obj, "doubler")