MediumJavaScriptTypeScript

Vue ref() — Reactive Primitive

VueVue.jsReactivityrefJavaScript

In Vue 3, reactive() works great for objects, but what about primitive values like numbers, strings, and booleans?

That's where ref() comes in. It wraps a primitive in a reactive container object with a single .value property:

const count = ref(0);

effect(() => {
  console.log('count:', count.value); // → "count: 0"
});

count.value = 1; // triggers the effect → "count: 1"
count.value = 2; // → "count: 2"

Implementation

The simplest correct implementation is just:

function ref(initialValue) {
  return reactive({ value: initialValue });
}

But you need to implement reactive() and effect() to make it work.

Your task

Implement ref(initialValue), effect(fn), and _reset().

The solve(op, ...args) harness is provided — do not modify it.

Sample tests

Test #1ref(42).value === 42
Input: ["get",42]
Output: 42
Test #2Setting .value is reflected immediately
Input: ["set",0,99]
Output: 99
Test #3effect() re-runs when ref.value changes
Input: ["track","hello","world"]
Output: "world"
Test #41 initial run + 3 mutation triggers = 4 total runs
Input: ["count",3]
Output: 4
Test #5An effect tracking r1 must NOT re-run when r2.value changes
Input: ["no-alias"]
Output: 1