Skip to content
Discussion options

You must be logged in to vote

Yes. In Svelte 5, idiomatic domain state is a class in a .svelte.ts file: $state per field, $derived for computed values, private fields with getters for encapsulation.

// dashboard.svelte.ts
export class DashboardState {
  #widgets = $state<Widget[]>([]);

  get widgets() { return this.#widgets; }
  visible = $derived(this.#widgets.filter(w => w.active));

  setRevenue(id: string, v: number) {
    const w = this.#widgets.find(w => w.id === id);
    if (w) w.revenue = v; // deep mutation, tracked per-property
  }
}

Consumers read dashboard.visible directly — no $ prefix, no subscriptions.

Rules:

  • $derived for computed values; getters for access only. Getters are reactive but uncached — th…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by samibennett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants