ReactiveVar
Documentation of ReactiveVar, a simple reactive variable package.
To use ReactiveVar
, add the reactive-var
package to your project by runningin your terminal:
meteor add reactive-var
Client
new ReactiveVar(initialValue, [equalsFunc])
import { ReactiveVar } from 'meteor/reactive-var' (reactive-var/reactive-var.js, line 37)
Constructor for a ReactiveVar, which represents a single reactive variable.
Arguments
- initialValueAny
The initial value to set.
equalsFunc
is ignored when setting the initial value.equalsFuncFunction
- Optional. A function of two arguments, called on the old value and the new value whenever the ReactiveVar is set. If it returns true, no set is performed. If omitted, the default
equalsFunc
returns true if its arguments are===
and are of type number, boolean, string, undefined, or null.
A ReactiveVar holds a single value that can be get and set, such that callingset
will invalidate any Computations that called get
, according to theusual contract for reactive data sources.
A ReactiveVar is similar to a Session variable, with a few differences:
ReactiveVars don’t have global names, like the “foo” in
Session.get('foo')
.Instead, they may be created and used locally, for example attached to atemplate instance, as in:this.foo.get()
.ReactiveVars are not automatically migrated across hot code pushes,whereas Session state is.
ReactiveVars can hold any value, while Session variables are limited toJSON or EJSON.
An important property of ReactiveVars — which is sometimes areason for using one — is that setting the value to the samevalue as before has no effect; it does not trigger any invalidations.So if one autorun sets a ReactiveVar, and another autorun gets theReactiveVar, a re-run of the first autorun won’t necessarily triggerthe second. By default, only primitive values are compared this way,while calling set
on an argument that is an object (not aprimitive) always counts as a change. You can configure this behaviorusing the equalsFunc
argument.
Client
ReactiveVar#get()
(reactive-var/reactive-var.js, line 62)
Returns the current value of the ReactiveVar, establishing a reactive dependency.
Client
ReactiveVar#set(newValue)
(reactive-var/reactive-var.js, line 74)
Sets the current value of the ReactiveVar, invalidating the Computations that called get
if newValue
is different from the old value.
Arguments
- newValueAny