GitHub v1.3.1

getters

Stable

Isomorphic, synchronous derivation of state. The only place logic belongs before rendering.

TypeScript Signature

typescript
Code copied!
  export const getters: Record<string, (context: { state: readonly<record<string, any>>;
    root: HTMLElement | null;
    refs: (id: string) => HTMLElement | null;
    slots: any;
    signal: AbortSignal;
  }) => any>
          </string,>

Isomorphic Execution

Getters are pure functions. They run during SSR (on the server) to generate the initial HTML, and they run again in the browser when reactive state changes. Because they must execute in both environments, they must be strictly synchronous.

CORALITE-E303: Asynchronous Getter

You cannot use async/await or return a Promise from a getter. If you need asynchronous data, fetch it in server() and place the result in state.

Smart State, Dumb Templates

Coralite templates do not allow complex expressions. You cannot write {{ firstName + ' ' + lastName }}. All logic must be lifted into a getter.

javascript
Code copied!
  export const getters = {
    fullName: ({ state }) => `${state.firstName} ${state.lastName}`
  }

No Passthrough Getters

Do not write a getter just to alias a state field. It adds unnecessary overhead and bloats the component. If the getter body is just state.foo, use the template token {{ foo }} directly.

Forbidden vs. Required

javascript
Code copied!
  export const getters = {
    // ❌ Passthrough (Dead code)
    displayName: ({ state }) => state.name
  }
html
Code copied!
  <!-- ✅ Direct token usage in template -->
  <span>{{ name }}</span>