Skip to main content
InterviewHubPRO
Section Overview
Q14

Question 14 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

28%
Svelte & SvelteKit (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

Universal Reactivity: Using Runes inside plain `.svelte.js` files and classes.

senior LevelCode ExampleSSR & HydrationSystem DesignSvelte 5

In Simple Words: In Svelte 4, sharing reactive state required writable() stores. In Svelte 5, you can use $state() directly inside regular JavaScript/TypeScript files (with a .svelte.ts extension):

typescript
// src/lib/cart.svelte.ts
export class ShoppingCart {
  items = $state<{ id: string; price: number }[]>([]);

  total = $derived(this.items.reduce((acc, item) => acc + item.price, 0));

  addItem(item: { id: string; price: number }) {
    this.items.push(item);
  }
}

export const cart = new ShoppingCart();