Rendering and Reaction
Ornata components react to state changes in a predictable sequence.
The update flow
Section titled “The update flow”When a state property changes after initialization, Ornata:
- validates the new value against the property options
- recomputes computed values
- runs render callbacks and applies DOM updates
- runs the matching watch callback
- notifies external state listeners registered with
addStateListener()
That means render and watch logic both respond to the same state transition, but they serve different purposes.
Use render for DOM output
Section titled “Use render for DOM output”render should be your primary way to reflect state into the DOM.
render: { panel() { return { attributes: { hidden: !this.state.open, }, classes: { "is-open": this.state.open, }, }; },}render: { panel() { return { attributes: { hidden: !this.state.open, }, classes: { "is-open": this.state.open, }, }; },}For a full walkthrough of text, html, attributes, classes, style, dataset, events, and list rendering with index, see Render Options.
Use watch for side effects
Section titled “Use watch for side effects”watch is best for logging, analytics, timers, imperative integrations, and other side effects.
watch: { open({ isInitial, newValue }) { if (!isInitial && newValue) { console.log("Disclosure opened"); } },}watch: { open({ isInitial, newValue }) { if (!isInitial && newValue) { console.log("Disclosure opened"); } },}For a deeper walkthrough of watcher behavior and isInitial, see Watchers.
Use computed for derived values
Section titled “Use computed for derived values”Computed values help when multiple render callbacks or methods need the same derived data.
computed: { countLabel() { return this.state.count === 1 ? "item" : "items"; },}computed: { countLabel() { return this.state.count === 1 ? "item" : "items"; },}Initial updates
Section titled “Initial updates”During mount, Ornata performs an initial update pass for each state property. That is why watch callbacks receive isInitial, and why render output is available immediately after mount.
External subscriptions
Section titled “External subscriptions”When code outside the component needs to react to public state changes, use addStateListener() on the mounted instance instead of putting that logic in watch.
See State Listeners for that pattern.