v0.6.22

Reactive Data

Last updated


This page explains how app.data detects changes, when a re-render is triggered, and which writes do not trigger one.

How It Works


The data option is wrapped recursively in a Proxy by createReactiveObject: reading a nested object returns a new Proxy, and a write whose new value differs from the old one (!==) tells the lifecycle to schedule an update.
  1. <div id="app">
  2. <p>{{ count }} · {{ user.name }} · {{ LENGTH(items) }}</p>
  3. <button @click="update">Update</button>
  4. </div>
  5. <script>
  6. const app = new QUI({
  7. id: "app",
  8. data: { count: 0, user: { name: "Pardn" }, items: [] },
  9. event: {
  10. update: () => {
  11. app.data.count++;
  12. app.data.user.name = "Chiu";
  13. app.data.items.push("A");
  14. },
  15. },
  16. when: {
  17. updated: () => console.log("updated", app.data.count),
  18. },
  19. });
  20. </script>
Live preview · @pardnchiu/quickui@0.6.22

All three writes trigger an update; writes within 300 ms of each other are merged into one render.

What Triggers an Update


WriteTriggers
app.data.key = value (value changes)Yes
Nested properties, array indexes, push / spliceYes
Writing the same valueNo
delete app.data.keyNo, the Proxy does not trap deletion
Replacing app.data = { ... }No, the replacement is not a Proxy
once: trueNo, data is not wrapped and renders once

When to Access It


app.data is assigned only after i18n finishes loading; even without i18n it is deferred to the next microtask. Values written synchronously right after new QUI() land on a placeholder object and are lost. Read and write it inside the rendered hook or an event handler.

Template Lookup Rules


RuleDescription
PathsUse . for nested values, for example {{ user.name }}
ScopeThe current :for item's data is searched first, then the root data
Missing valueThe {{ }} placeholder stays on the page as written
Usable typesStrings, numbers, booleans, arrays, plain objects; functions and Date are lost in the snapshot
中文