How Reactivity in Vue and Svelte are different?

brandonwie

Brandon Wie

Posted on July 25, 2024

How Reactivity in Vue and Svelte are different?

Vue and Svelte indeed have different approaches to reactivity. Here's a refined explanation of their key differences:

Vue's Reactivity:

Uses a proxy-based reactivity system (in Vue 3) or getter/setter-based system (in Vue 2). Requires special APIs like ref() or reactive() to make data reactive. Operates at runtime, wrapping reactive objects with JavaScript Proxies (in Vue 3).

Svelte's Reactivity:

Takes a compile-time approach to reactivity.
Uses two main mechanisms for reactivity:

  1. Assignment-based reactivity for component state.
  2. Reactive declarations for derived values.

Key Differences:

Reactive State:

  • Vue: Requires ref() or reactive() to create reactive state.
  • Svelte: Uses regular JavaScript variables for component state.

Derived Values:

  • Vue: Uses computed properties or watchers.
  • Svelte: Uses reactive declarations with the $: label.

Update Mechanism:

  • Vue: Updates at runtime based on Proxy interceptions.
  • Svelte: Generates efficient update code at compile-time.

Let's look at examples that showcase these differences:

Vue 3:

<script setup>
import { ref, computed } from 'vue'

const count = ref(0);
const doubleCount = computed(() => count.value * 2);

function increment() {
  count.value++;
}
</script>

<template>
  <button @click="increment">Count is: {{ count }}</button>
  <p>Double count: {{ doubleCount }}</p>
</template>
Enter fullscreen mode Exit fullscreen mode

Svelte:

<script>
let count = 0;
$: doubleCount = count * 2;

function increment() {
  count += 1;
}
</script>

<button on:click={increment}>Count is: {count}</button>
<p>Double count: {doubleCount}</p>
Enter fullscreen mode Exit fullscreen mode

In the Svelte example, note how we use:

  1. A regular variable count for the state.
  2. A reactive declaration $: doubleCount = count * 2 for derived values.

This $: syntax is crucial in Svelte. It tells the compiler to re-run the statement whenever its dependencies change. This is how Svelte handles computations that depend on reactive state, similar to Vue's computed properties but with a different syntax and implementation.
Svelte's approach allows for more than just simple computations. You can have complex logic in reactive declarations:

<script>
export let data;
$: wordCount = data.content.split(' ').length;
$: estimatedReadingTime = wordCount / 250;
</script>
Enter fullscreen mode Exit fullscreen mode

This demonstrates how Svelte can handle derived values and side effects in a way that feels more like writing regular JavaScript, while still maintaining reactivity.

In summary, while both Vue and Svelte achieve reactivity, they do so through fundamentally different mechanisms. Vue's system is more explicit and operates at runtime, while Svelte's is more implicit and leverages compilation to achieve its reactivity.

References

*Photo by Ferenc Almasi on Unsplash

💖 💪 🙅 🚩
brandonwie
Brandon Wie

Posted on July 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related