Skip to content

ReactiveVar

要使用 ReactiveVar,请在终端中运行将 reactive-var 包添加到你的项目中:

¥To use ReactiveVar, add the reactive-var package to your project by running in your terminal:

bash
meteor add reactive-var

ReactiveVar

Client only

Summary:

Constructor for a ReactiveVar, which represents a single reactive variable.

Arguments:

Source code
NameTypeDescriptionRequired
initialValueAny

The initial value to set. equalsFunc is ignored when setting the initial value.

Yes
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.

No
js
import { ReactiveVar } from "meteor/reactive-var"";

const reactiveVar = new ReactiveVar();
  any,
() => {}, // this param is optional
);

ReactiveVar 包含一个可以获取和设置的值,这样调用 set 将使任何调用 get 的计算无效,这符合通常的反应数据源合同。

¥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.

ReactiveVar 类似于 Session 变量,但有一些不同:

¥A ReactiveVar is similar to a Session variable, with a few differences:

  • ReactiveVars 没有全局名称,如 Session.get('foo') 中的 "foo"。相反,它们可以在本地创建和使用,例如附加到模板实例,如下所示:this.foo.get()

    ¥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 不会在热代码推送之间自动迁移,而 Session 状态则会。

    ¥ReactiveVars are not automatically migrated across hot code pushes, whereas Session state is.

  • ReactiveVars 可以保存任何值,而 Session 变量仅限于 JSON 或 EJSON。

    ¥ReactiveVars can hold any value, while Session variables are limited to JSON or EJSON.

ReactiveVars 的一个重要属性 — — 有时这也是使用它的原因 — — 是将值设置为与之前相同的值没有效果;它不会触发任何无效。因此,如果一个自动运行设置了 ReactiveVar,而另一个自动运行获得了 ReactiveVar,则重新运行第一个自动运行不一定会触发第二个自动运行。默认情况下,只有原始值以这种方式比较,而对对象(非原始)参数调用 set 始终算作更改。你可以使用 equalsFunc 参数配置此行为。

¥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

Summary:

Returns the current value of the ReactiveVar, establishing a reactive dependency.

js

// reactiveVar is an instance of ReactiveVar

const result = reactiveVar.get();

reactiveVar.set

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 code
NameTypeDescriptionRequired
newValueAny----Yes
js

// reactiveVar is an instance of ReactiveVar

const result = reactiveVar.set();
  any
);