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
The initial value to set
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:The new value to set
Default Equality
The default equality function (ReactiveVar._isEqual):
numberbooleanstringundefinednull
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
Debugging
Related Packages
reactive-dict
Reactive key-value dictionaries
tracker
Reactive computation system
session
Global reactive state