Reactivity
elements man html/reactivityHow the runtime decides what to update when data changes, and the $ handle
for inspecting a live template from the browser console.
Reactivity in templates follows the normal JavaScript distinction between reference and value semantics:
- Objects and arrays pass by reference. Mutating properties or array entries inside an event handler propagates back to the template.
- Primitives pass by value. A handler cannot mutate the caller's variable directly. Use one of the two patterns below to update a primitive attribute from a handler.
(a) Return-value. Handler returns the new value, template assigns it. Sync, pure transforms.
Handlerfunction trim(value: string): string { return value.trim(); }
Template<input onblur={() => name = trim(name)}>
(b) Callback. Handler invokes a setter. Async, conditional, or updates more than one attribute.
Handlerfunction onSubmit(form: Form, resetUI: () => void) { resetUI(); saveUser(form); }
Template<form onsubmit={() => onSubmit(form, () => form = empty())}>
Tip: when several primitive attributes change together, you can bundle them
into a single private object attribute. The object passes by reference, so
handlers can mutate fields directly:
Object attributeinterface FormFields { text: string; tag: string; } <Form ( private form: FormFields = { text: "", tag: "", }, )>
Objects in State Are Wrapped
In the browser, a plain object or array in a template attribute is wrapped in a Proxy so its reads and writes can be tracked. Reading it back gives the wrapper, not the object you stored, so an identity check fails:
Identity<Grid (private v: { row: Row } = { row: lib.rows[0] })> <p>{String(v.row === lib.rows[0])}</p> </Grid>
The paragraph reads false in the browser. Values compare as before, and a
class instance is never wrapped. It matters when you hand the object to code
that compares by identity, such as a library looking up its own rows.
unwatched(x) from @elements/app gives back the object under the wrapper:
Unwrapimport { unwatched } from "@elements/app"; lib.isSelected(unwatched(v.row));
Better still, keep a library's own objects out of template state. Store the id and ask the library for the object when you need it. On the server nothing is wrapped.
Because of that, an identity check against an object from state can give one answer in the server render and another in the browser, and the two renders disagree. Compare by id instead:
Compare by id<li e:for={t of tabs} class={[t.id === v.active.id && "is-active"]}>{t.name}</li>
$ in the Browser Console
You can use $ in the browser devtools console to set any of the page's HTML
template attributes and watch the UI update. $ is the page's HTML template
attributes object, and assigning to any property goes through the same reactive
pipeline the template uses. This helps with debugging and with exploring page
states without clicking through the UI:
Browser console$.title = "new title"; // re-renders the title $.users = Array(100).fill(...); // re-renders the list with 100 rows $.error = "out of stock"; // flips into the error state $.theme = "dark"; // switches the theme
You can jump to any reactive state you can construct, without touching the source or rebuilding.