ReactiveVar
To use ReactiveVar
, add the reactive-var
package to your project by running in your terminal:
meteor add reactive-var
ReactiveVar Client only
Client only
Summary:
Constructor for a ReactiveVar, which represents a single reactive variable.
Arguments:
Source codeName | Type | Description | Required |
---|---|---|---|
initialValue | Any | The initial value to set. | Yes |
equalsFunc | function | 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 | No |
import { ReactiveVar } from "meteor/reactive-var"";
const reactiveVar = new ReactiveVar(
any,
() => {}, // this param is optional
);
A ReactiveVar holds a single value that can be get and set, such that calling set
will invalidate any Computations that called get
, according to the usual 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 a template 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 to JSON or EJSON.
An important property of ReactiveVars — which is sometimes a reason for using one — is that setting the value to the same value as before has no effect; it does not trigger any invalidations. So if one autorun sets a ReactiveVar, and another autorun gets the ReactiveVar, a re-run of the first autorun won't necessarily trigger the second. By default, only primitive values are compared this way, while calling set
on an argument that is an object (not a primitive) always counts as a change. You can configure this behavior using the equalsFunc
argument.
reactiveVar.get Client only
Client only
Summary:
Returns the current value of the ReactiveVar, establishing a reactive dependency.
// reactiveVar is an instance of ReactiveVar
reactiveVar.get();
reactiveVar.set Client only
Client only
Summary:
Sets the current value of the ReactiveVar, invalidating the Computations that called get
if newValue
is different from the old value.
Arguments:
Source codeName | Type | Description | Required |
---|---|---|---|
newValue | Any | ---- | Yes |
// reactiveVar is an instance of ReactiveVar
reactiveVar.set(
any
);