Skip to main content
ReactiveVar provides a single reactive value that can be get and set, automatically invalidating computations that depend on it.

Installation

Overview

ReactiveVar is:
  • A container for a single reactive value
  • Similar to Session variables but scoped (not global)
  • More flexible than Session (can hold any value, not just EJSON)
  • Integrated with Tracker for automatic reactivity
  • Efficient at absorbing unnecessary invalidations

Package Information

  • Version: 1.0.13
  • Summary: Reactive variable
  • Dependencies: tracker
  • Export: ReactiveVar

Basic Usage

Creating a ReactiveVar

Getting Values

Setting Values

Reactive Updates

ReactiveVar automatically invalidates computations:

Constructor

initialValue
any
required
The initial value to set
equalsFunc
function
Optional function to determine if values are equal. Called with (oldValue, newValue). If it returns true, no invalidation occurs.

Custom Equality Function

By default, ReactiveVar uses a conservative equality check:

Methods

get()

Returns the current value and registers a dependency:
Calling get() inside a reactive computation (like Tracker.autorun) establishes a dependency.

set(newValue)

Sets a new value and invalidates dependents if changed:
newValue
any
required
The new value to set

Default Equality

The default equality function (ReactiveVar._isEqual):
The default check only considers primitive values equal:
  • number
  • boolean
  • string
  • undefined
  • null

Patterns and Examples

Pattern: Component State

Pattern: Loading State

Pattern: Form State

Pattern: Derived ReactiveVars

Pattern: Toggle State

Pattern: Array Modifications

Since arrays are objects, you need to handle them specially:

Comparison with Session

ReactiveVar

Session

Using with React

Using with Blaze

Performance Considerations

ReactiveVar is lightweight - Very little overhead compared to Session
Absorbs redundant updates - Setting the same primitive value multiple times doesn’t cause reruns
Object/Array updates - Always trigger invalidations unless using custom equality

Debugging

reactive-dict

Reactive key-value dictionaries

tracker

Reactive computation system

session

Global reactive state

Source Code