Denis Jakus

making tech less cryptic

CTO x Unique People

Denis Jakus

making tech less cryptic

CTO x Unique People

CTO Note

Reactive user interfaces using SvelteJS6 min read

19 June 2019 Javascript
Reactive user interfaces using SvelteJS6 min read

Today I’ll be writing about SvelteJS.

A truly refreshing Javascript framework (Well, Svelte is not a framework. It’s a compiler.) which will, as stated on web, enhance you web apps.

But I must say, not just your web apps, your whole experience of developing, will be enhanced.

Only few..ermmm, frameworks (should I call it a framework, NO!!!) blew my mind and this is one of them.

Why is that the case?

Let’s sum up the whole framework in a few bulletins and then I’ll show you some examples:

  1. fast when executing
  2. easy to grasp
  3. reactive without any fuss

Fast when executing

Since it’s not a framework but a compiler, it compiles our code and makes the most optimized Javascript code as a result.

It does not have virtual DOM.

Comparing it with other frameworks, you will see soon enough how small the production output of .js file really is.

Just try and compare it with React, VueJS or Angular when starting the blank project.

So here is a small task for you:

Take a look at the kb you get without writing any custom code!

With SvelteJS, blank project is roughly around 4kb. So, compare that to other frameworks!

Easy to grasp

Well, it really is EASY to get into Svelte world and the documentation is pretty solid.

But real magic happens when you start to dive into Svelte by writing apps. That was when I got blown away.

Svelte is component based, as any other framework.
You write everything into one file, just like in VueJS/React i.e.:

// app.svelte file
// this is where all of your code for the app component goes in
// here you do the stylings
// plain html stuff goes here

Writing a component is easy. Just name a file i.e. Button.svelte and then you need to import it into other component.

// other.svelte file
import Book from './Book.svelte';

If you, for instance, want to send text inside button component, we use plain html syntax (slot) i.e.

// custom.svelte file
import Button from "./Button.svelte";
<Button>Some text</Button>
// Button.svelte 

Component communication using props

Defining a props is pretty simple. Just add export in front of a variable you want to behave as a prop or input for a component.

// Book.svelte file
export let bookTitle; // makes this as a props - an input for this component
export let bookAuthor; // makes this as a props - an input for this component
export let bookDescription; // makes this as a props - an input for this component
<h1> {bookTitle} </h1>
<h1> {bookAuthor} </h1>
<h1> {bookDescription} </h1>

This way we can then use our Book component inside other custom component like this:

import Book from './Book.svelte';
<Book bookTitle="title 1" bookAuthor="author name" bookDescription="lorem ipsum" />

Let’s talk about binding.
We use curly { } braces to output or send values as input to other components.

// we bind model so we could output variable value here
<input type="text" id="title" value="{bookTitle}"/> 
// we bind event listeners (click, blur, input..) using normal html syntax with 'on:' 
// this is an example of two way binding
<input type="text" id="title" value="{bookTitle}" on:input="{nameOfJavascriptFunction}"/> 
// above example can be simplified with "bind" directive
<input type="text" id="title" bind:value="{bookTitle}"/>   

If you want to communicate between components(i.e.Button and Book) component, the easiest way would be by using dispatch.

  • Step 1. we set trigger on, without a pointer to any function
  • Step 2. in Book component we use that same trigger to dispatch any value
  • Step 3. we subscribe to an event dispatched, in a component we want to catch that event with value
// Step 1.
// Button.svelte component
<button on:click>
// Step 2.
// Book.svelte file
import {createEventDispatcher} from 'svelte';
import Button from './Button.svelte';
export let bookTitle;
export let bookAuthor;
export let bookDescription;
const dispatch = createEventDispatcher();
function showAuthorBooks() {
dispatch('showauthorbooks', bookAuthor);
<h1> {bookTitle} </h1>
<h2> {bookAuthor}</h2>
<p> {bookDescription} </p>
<Button on:click="{showAuthorBooks}">Show all author books</Button>
// Step 3.
// Custom.svelte file
import Book from "./Book.svelte";
function addToCart(event){
const bookAuthor = event.detail;
<Book showauthorbooks="{showAuthorBooks}"/>

Arrays handling

Svelte only updates when we have ‘=‘ sign to a variable.
So if we initialized array like:

 books = [];

we set variable books with pointer to an array.
It will only update books but not the pointer to array.
So when we use:


it wont automatically update books.
The solution for this is to use array.concat ie.:

books = books.concat(newBook);

As i said before, this way by using ‘=’ we assign the value to an array and array get’s updated.

Reactive without any fuss

Dynamic / Labeled statements

These statements are used for computing values. As soon as we declare it after $: , that declaration becomes variable which can be used inside component. Hence, reactivity!

// labeled statements
// in svelte will be used to reexecuted when inputs changes
$: calculatedItems= items.reduce((sum, curValue) => {
return sum + curValue.price;
<h1>Calculated value: ${calculatedItems}</h1>


When mixing code with html in Svelte, we use syntax like this:

{#if items.length === 0}
<p>No yet.</p>
{#each items as item}
<li>{item.title} - ${item.price}</li>    

For more examples, please take a look at Svelte documentation.

Animating html elements

Easy, just see this example:

<p in:fly={{ x: 2000, duration: 500}} out:fly={{ x:-500, duration:500}}>
Some text that will appear using fly in and disappear using fly out animation

Using promises

This is just a simple overview of how it work’s in Svelte.

First create logic behind promise.

function myPromise() {
return new Promise();
let customPromise = myPromise().then(r=>Math.random());

Secondly, show results of promise inside template.

{#await customPromise}
<p> show while promise is resolved</p>
{:then calculatedNumber}
<p> show result of resolved promise: {calculatedNumber} </p>
{:catch error}
<p> {error.message}


This is only tip of an iceberg.
As you can see, Svelte is joy to use. I will surely cover it up more in upcoming posts.

If you want to get more into Svelte, subscribe to mailing list to get notified of my upcoming video course about it!

Related Posts
Learn how to use async inside useEffect hook

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like Did you stumble upon a…

Javascript Splice VS Javascript Slice

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like Little chit-chat Hi! Let me…

Write a comment