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 the computed function

Or,

  • _1
  • _2
  • target (Object): an object that has a computed property, created by @computed or extendObservable
  • 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")

results matching ""

    No results matching ""