Reading properties from the returned proxy within a reactive scope (like one created by
$ or observe) establishes a subscription. Modifying properties through
the proxy will notify subscribed scopes, causing them to re-execute.
Plain objects and arrays are wrapped in a standard JavaScript Proxy that intercepts
property access and mutations, but otherwise works like the underlying data.
Primitives (string, number, boolean, null, undefined) are wrapped in an object
{ value: T } which is then proxied. Access the primitive via the .value property.
Use unproxy to get the original underlying data back.
Param: target
The object, array, or primitive value to make reactive.
Returns
A reactive proxy wrapping the target data.
Template: T
The type of the data being proxied.
Example: Object
conststate = proxy({ count:0, message:'Hello' }); observe(() =>console.log(state.message)); // Subscribes to message setTimeout(() =>state.message = 'World', 1000); // Triggers the observe function setTimeout(() =>state.count++, 2000); // Triggers nothing
Example: Array
constitems = proxy(['a', 'b']); observe(() =>console.log(items.length)); // Subscribes to length setTimeout(() =>items.push('c'), 2000); // Triggers the observe function
Example: Primitive
constname = proxy('Aberdeen'); observe(() =>console.log(name.value)); // Subscribes to value setTimeout(() =>name.value = 'UI', 2000); // Triggers the observe function
Creates a reactive proxy around the given data.
Reading properties from the returned proxy within a reactive scope (like one created by $ or observe) establishes a subscription. Modifying properties through the proxy will notify subscribed scopes, causing them to re-execute.
Proxy
that intercepts property access and mutations, but otherwise works like the underlying data.{ value: T }
which is then proxied. Access the primitive via the.value
property.Use unproxy to get the original underlying data back.
Param: target
The object, array, or primitive value to make reactive.
Returns
A reactive proxy wrapping the target data.
Template: T
The type of the data being proxied.
Example: Object
Example: Array
Example: Primitive
Example: Class instance