Lädt...


🔧 How Reactivity in Vue and Svelte are different?


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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>

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>

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>

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

...

🔧 How Reactivity in Vue and Svelte are different?


📈 58.76 Punkte
🔧 Programmierung

🔧 Vue.js Fetch API | Search Vue.js | Sort Vue.js | Pagination Vue.js | Filter Vue.js


📈 56.45 Punkte
🔧 Programmierung

🔧 Learning the new Svelte v5 Reactivity System


📈 36.63 Punkte
🔧 Programmierung

🔧 An Animated Guide to Vue 3 Reactivity and Internals


📈 33.92 Punkte
🔧 Programmierung

🔧 Moving From Vue 1 To Vue 2 To Vue 3: A Case Study Of Migrating A Headless CMS System


📈 33.87 Punkte
🔧 Programmierung

📰 Vue.js 3: Reactivity System und Composition API unter der Lupe


📈 32.66 Punkte
📰 IT Nachrichten

🔧 Vue's reactivity is a trap


📈 32.66 Punkte
🔧 Programmierung

🔧 Vue reactivity for impatient devs


📈 32.66 Punkte
🔧 Programmierung

🔧 Vue 3 da reaktivlik (reactivity)


📈 32.66 Punkte
🔧 Programmierung

🔧 How to Build Your Own Vue-like Reactivity System from Scratch


📈 32.66 Punkte
🔧 Programmierung

🔧 Vue 3 | Cheat Sheet of The Dark Side | Part 1 | Reactivity


📈 32.66 Punkte
🔧 Programmierung

🔧 Vue.js Reactivity Fundamentals : Composition API


📈 32.66 Punkte
🔧 Programmierung

🔧 Vue.js Reactivity Fundamentals : Composition API


📈 32.66 Punkte
🔧 Programmierung

🔧 Understanding Vue Reactivity with Pinia Stores


📈 32.66 Punkte
🔧 Programmierung

🔧 Understanding Svelte: Setting Up Your Project and Mastering .svelte Files


📈 31.77 Punkte
🔧 Programmierung

🔧 From Svelte 4 to Svelte 5: Understanding Slots (default and named)


📈 31.77 Punkte
🔧 Programmierung

🔧 From Svelte 4 to Svelte 5: Understanding Slots (default and named)


📈 31.77 Punkte
🔧 Programmierung

🕵️ Medium CVE-2021-29261: Svelte Svelte


📈 30.51 Punkte
🕵️ Sicherheitslücken

🔧 Spice Up Your Svelte App with Sound Interactions using svelte-sound 😎


📈 30.51 Punkte
🔧 Programmierung

🔧 kbar-svelte-mini - ctrl+k menu for your Svelte website


📈 30.51 Punkte
🔧 Programmierung

🔧 Svelte Series-2: How to install Svelte


📈 30.51 Punkte
🔧 Programmierung

🔧 Svelte + Manifest = Giving Svelte a proper backend with 7 lines of code 🧡🦚


📈 30.51 Punkte
🔧 Programmierung

🔧 Exploring Svelte and Vue.js: A Newbie's Perspective on Modern Frontend Technologies.


📈 27.8 Punkte
🔧 Programmierung

🔧 Comparing Svelte and Vue.js: A Tale of Two Frontend Technologies


📈 27.8 Punkte
🔧 Programmierung

🔧 A Comparative Analysis of Svelte and Vue.js: Exploring Niche Frontend Technologies


📈 27.8 Punkte
🔧 Programmierung

🔧 Comparing Svelte and Vue.js: A Battle of Frontend Technologies


📈 27.8 Punkte
🔧 Programmierung

🔧 Why Do People Really Love These Frameworks: Angular, React, Vue, and Svelte?


📈 27.8 Punkte
🔧 Programmierung

🔧 The Future of Web Development: A Dive into React, Next.js, Svelte, and Vue.js


📈 27.8 Punkte
🔧 Programmierung

🔧 The Evolution of JavaScript Frameworks: From jQuery to Svelte, with React and Vue Along the Way


📈 27.8 Punkte
🔧 Programmierung

🔧 ALLAIS: Create UI for React Svelte and Vue


📈 27.8 Punkte
🔧 Programmierung

📰 heise+ | JavaScript-Frameworks: Angular, React, Vue und Svelte im Vergleich


📈 26.55 Punkte
📰 IT Nachrichten

🔧 Making Your Choice: Svelte, React or Vue?


📈 26.55 Punkte
🔧 Programmierung

🔧 Exploring Frontend Technologies: Svelte vs. Vue.js


📈 26.55 Punkte
🔧 Programmierung

matomo