Use this file to discover all available pages before exploring further.
ReactiveDict provides a reactive dictionary of key-value pairs, similar to Session but scoped to your usage. It’s ideal for component-level reactive state.
import { ReactiveDict } from 'meteor/reactive-dict';// Anonymous dictionary (not persisted)const state = new ReactiveDict();// Named dictionary (persisted across hot code push)const settings = new ReactiveDict('userSettings');// With initial valuesconst form = new ReactiveDict('formData', { username: '', email: '', subscribed: false});
// Set a single keystate.set('username', 'alice');state.set('count', 42);state.set('user', { name: 'Alice', id: 123 });// Set multiple keys at oncestate.set({ username: 'alice', email: 'alice@example.com', role: 'admin'});
// Get a single value (reactive)const username = state.get('username');console.log(username); // 'alice'// Get with default valueconst theme = state.get('theme') || 'light';// Get all valuesconst allData = state.all();console.log(allData); // { username: 'alice', count: 42, ... }
// Persisted across hot code push (client only)const appState = new ReactiveDict('appState');// After hot code push, values are restoredappState.set('theme', 'dark');// Hot code push happens...console.log(appState.get('theme')); // Still 'dark'
dict.setDefault('count', 0);dict.setDefault('theme', 'light');// If 'count' already exists, it won't be changeddict.set('count', 5);dict.setDefault('count', 10); // count is still 5
ReactiveDict invalidates only computations that depend on changed keys:
const dict = new ReactiveDict();dict.set('a', 1);dict.set('b', 2);// This only reruns when 'a' changesTracker.autorun(() => { console.log('A:', dict.get('a'));});// This only reruns when 'b' changesTracker.autorun(() => { console.log('B:', dict.get('b'));});dict.set('a', 3); // Only first autorun rerunsdict.set('b', 4); // Only second autorun reruns
Named ReactiveDict instances are automatically preserved across hot code push:
// Client only - persisted across reloadsconst settings = new ReactiveDict('userSettings');settings.set('theme', 'dark');settings.set('language', 'en');// After hot code push, values are restored
Persistence only works on the client and only for hot code push, not full page reloads.
const dict = new ReactiveDict();dict.set('key', 'value');const value = dict.get('key');// ✓ Multiple instances// ✓ Scoped (not global)// ✓ Must be passed around// ✓ Optional persistence with naming// ✓ Better for component state
Session.set('key', 'value');const value = Session.get('key');// ✓ Global (one instance)// ✓ Accessible everywhere// ✓ Always persisted// ✓ Can have key collisions// ✓ Harder to reason about
Use equals() for boolean checks - More efficient than get() when checking specific values
Avoid all() in computations - Creates dependency on every key
Batch updates - Set multiple keys at once with object syntax
// Good: Single reactivity invalidationdict.set({ a: 1, b: 2, c: 3});// Less efficient: Three separate invalidationsdict.set('a', 1);dict.set('b', 2);dict.set('c', 3);