Being Reactive - Say NO to Virtual DOM, Meet Svelte

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Nice article! Honestly hoping Svelte will become the 'new big thing' in the frontend world. It's approach of keeping components in one file is similar to Vue is really neat which will make easier for some to try it out without sinking in docs. The only thing that is missing is an official router but that never was a problem for React and I've seen few non-official ones which are pretty good. Let's just hope it won't go unnoticed.
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
Sounds Strange? Welcome to the Second Post of the series called, 'Being Reactive'.
If you haven't read the First Post on Virtual DOM and DOM Diffing yet, please read through. In the last post, I talked about Virtual DOM, DOM Diffing, and the cost associated with it.
Many of the modern frameworks and libraries are using the mechanism of Virtual DOM to get what has been changed and make the application react(or update) to it.
Virtual DOM is a superb concept. But, the cost of Comparison and Reconciliation is hefty for browsers and the garbage collection cycle. Performing the DOM change comparisons and DOM update at the run time, the framework actually runs in the Browser. This leads to performance issues and other overheads.
Reactive Programming concepts, and the asynchronous data flow implementation has been taken care of at build time than left it for run time?How does it sound? Fascinating?
Image Courtesy: GIphy.com
Svelte is the new guy on the block with a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue do the bulk of their work in the browser, Svelte shifts that work into a compile step that happens when you build your app. Isn't that cool?

As per https://svelte.dev/:
Instead of using techniques like virtual DOM diffing, Svelte writes code that surgically updates the DOM when the state of your app changes.
With Svelte, you write your components using HTML, CSS, and JavaScript. During your build process Svelte compiles them into tiny standalone JavaScript modules. The build process takes care of all that could detect changes and do a proper state update. You as a developer and your app users are at great advantages here. Why?
Svelte app is much faster than the apps created using React, Angular, Vue, etc. Simply because it is nothing but the vanilla JS.Svelte is a vanilla JS at the end of the day. A component written in Svelte can be used with any project written in any other Web Framework or Library.Here is an example is taken from https://svelte.dev/ where it showcases the simplicity of developing a Component.
<script>
let a = 1;
let b = 2;
</script>
<input type="number" bind:value={a}>
<input type="number" bind:value={b}>
<p>{a} + {b} = {a + b}</p>
As you guessed it right, this simple component binds two values with two text boxes. On updating the text box values, the application reacts and changes the output value within the paragraph(p) element. Here is the output:

Curious how the ReactJs equivalent would look like? It just has many more lines :
import React, { useState } from 'react';
export default () => {
const [a, setA] = useState(1);
const [b, setB] = useState(2);
function handleChangeA(event) {
setA(+event.target.value);
}
function handleChangeB(event) {
setB(+event.target.value);
}
return (
<div>
<input type="number" value={a} onChange={handleChangeA}/>
<input type="number" value={b} onChange={handleChangeB}/>
<p>{a} + {b} = {a + b}</p>
</div>
);
};
It would be a similar case with Angular, Vue, etc as well.
Here is my first attempt to create a Svelte Component and I am still exploring and less judging:

Interested to join the Journey? Here are a few important links that will help in exploring Svelte:
So far, the difficult most part with Svelte is, to Pronounce it correctly 😃. In case you have the same struggle, here is a help for you:
Hope you liked the post. Please hit the Follow button below to read my future articles. Happy Exploring!