Skip to content

Computed

computed is where Ornata components define derived values.

If state is the source of truth, computed is where you turn that state into values the rest of the component can read conveniently.

Computed values help when:

  • multiple render callbacks need the same derived value
  • a method needs a value derived from several state properties
  • you want derived logic in one named place instead of repeating it inline
computed: {
countLabel() {
return this.state.count === 1 ? "item" : "items";
},
}

When state changes, Ornata recomputes every computed property.

Each computed callback receives a context object with:

  • type
  • currentValue
  • changedProperty

currentValue is the previously computed value for that property.

changedProperty is the state key that triggered the recomputation.

That context is especially useful when a computed value is expensive to derive. You can check whether the changed state is actually relevant before redoing heavier work.

computed: {
summary({ currentValue, changedProperty }) {
console.log("Was:", currentValue);
console.log("Triggered by:", changedProperty);
return `${this.state.count} clicks`;
},
}

For simple computed values, you can usually ignore the context object.

For more expensive computed values, currentValue and changedProperty let you skip work when the updated state does not affect the result.

computed: {
filteredItems({ currentValue, changedProperty }) {
if (
changedProperty !== "query" &&
changedProperty !== "items"
) {
return currentValue ?? [];
}
return this.state.items.filter((item) =>
item.label.includes(this.state.query)
);
},
}

This gives the component a chance to reuse the current computed value when unrelated state changes trigger the broader update flow.

Computed values are especially helpful when several parts of the component need the same derived data.

computed: {
isOpen() {
return this.state.open && this.state.enabled;
},
},
render: {
panel() {
return {
attributes: {
hidden: !this.computed.isOpen,
},
};
},
},
methods: {
announce() {
console.log(this.computed.isOpen);
},
}
  • use computed for derived values
  • use watch for side effects
  • use data for persistent non-reactive storage
  • use render for DOM output

If state is what the component knows, computed is what the component can infer from what it knows.