Skip to main content
InterviewHubPRO
Section Overview
Q36

Question 36 of 50

Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

72%
Svelte & SvelteKit (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

Svelte 5 Runes Deep Dive: `$state`, `$derived`, `$effect`, `$props`, and `$bindable`.

senior LevelCode ExampleComponentsSystem DesignSvelte 5

In Simple Words: Svelte 5 replaced compilation-based let assignments and $: reactive declarations with Runes (function-like compiler macros prefixed with $) for explicit, scalable fine-grained reactivity:

  • $state(initial): Declares a reactive state variable or deep reactive proxy object.
  • $derived(expression): Replaces $: double = count * 2, creating a cached derived computation.
  • $effect(fn): Replaces $: { ... } for side effects, executing after the DOM updates with automatic dependency tracking.
  • $props(): Declares component input props with TypeScript types.
  • $bindable(): Marks a prop as eligible for two-way bind:prop binding by parent components.
svelte
<!-- Svelte 5 Component using Runes -->
<script lang="ts">
  let { title = 'Counter', count = $bindable(0) } = $props<{ title?: string; count?: number }>();

  let double = $derived(count * 2);

  $effect(() => {
    console.log(`Count changed to ${count}, double is ${double}`);
    return () => console.log('Cleanup before next effect or teardown');
  });
</script>

<button onclick={() => count++}>{title}: {count} (Double: {double})</button>