Reading properties from the returned proxy within a reactive scope (like one created by
$ or derive) 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.
Promises are represented by proxied objects { busy: boolean, value?: T, error?: any }.
Initially, busy is true. When the promise resolves, value is set and busy
is set to false. If the promise is rejected, error is set and busy is also
set to false.
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' }); $(() =>console.log(state.message)); // Subscribes to message setTimeout(() =>state.message = 'World', 1000); // Triggers the observing function setTimeout(() =>state.count++, 2000); // Triggers nothing
Example: Array
constitems = proxy(['a', 'b']); $(() =>console.log(items.length)); // Subscribes to length setTimeout(() =>items.push('c'), 2000); // Triggers the observing function
Example: Primitive
constname = proxy('Aberdeen'); $(() =>console.log(name.value)); // Subscribes to value setTimeout(() =>name.value = 'UI', 2000); // Triggers the observing function
Creates a reactive proxy around the given data.
Reading properties from the returned proxy within a reactive scope (like one created by $ or derive) establishes a subscription. Modifying properties through the proxy will notify subscribed scopes, causing them to re-execute.
Proxythat intercepts property access and mutations, but otherwise works like the underlying data.{ value: T }which is then proxied. Access the primitive via the.valueproperty.{ busy: boolean, value?: T, error?: any }. Initially,busyistrue. When the promise resolves,valueis set andbusyis set tofalse. If the promise is rejected,erroris set andbusyis also set tofalse.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