Thanks to visit codestin.com
Credit goes to www.slideshare.net

Building efficient frontends
with Svelte
Mikhail Kuznetcov
ING Investments
1
Agenda
● Why Svelte
● Svelte basics
● Code demo
● Advanced usage & links
2
Why Svelte
01
History
4
Author: Richard Harris (@rich_harris)
Creator or Rollup.js etc.
● First release: end of 2016
● v2 - “Magical disappearing
framework”
● v3 - “Cybernetically enhanced web
apps”
● Current version: 3.x
Library size
5
React Vue Svelte
>100KB >80KB <3KB
6
@TheLarkInn
Library speed
7
Source: https://github.com/krausest/js-framework-benchmark
Amount of code
8
<template>
<div>
<input type="number" v-model.number="a">
<input type="number" v-model.number="b">
<p>{{a}} + {{b}} = {{a + b}}</p>
</div>
</template>
<script>
export default {
data () {
return {
a: 1,
b: 2
}
}
}
</script>
import React, { useState } from 'react'
export default () => {
const [a, setA] = useState(1)
const [b, setB] = useState(2)
function handleA(event) {
setA(+event.target.value)
}
function handleB(event) {
setB(+event.target.value)
}
return (
<div>
<input type="number" value={a} onChange={handleA}/>
<input type="number" value={b} onChange={handleB}/>
<p>{a} + {b} = {a + b}</p>
</div>
)
}
<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>
● Compiler centered
● No VirtualDOM overhead
● Simpler, native reactivity
● Small bundles, faster app code
Key differences
9
Svelte basic API
02
Project setup
11
$ npx degit sveltejs/template my-app
(same as git clone https://github.com/sveltejs/template)
$ cd my-app
$ npm install && npm run dev
Hello world
12
<script>
export let name
</script>
<style>
h1 {
color: purple;
}
</style>
<h1>Hello {name}!</h1>
Template syntax
13
{#if time < 10}
<p>Good morning</p>
{:else if time < 20}
<p>Good day</p>
{:else}
<p>Good evening</p>
{/if}
{#if awesome}
<p>Svelte is awesome!</p>
{/if}
<h1>Users list</h1>
<ul>
{#each users as user}
<li>{user.name} - {user.age}</li>
{/each}
</ul>
Events
14
<script>
let count = 0
function handleClick(event) {
count += 1
}
</script>
<button on:click={handleClick}>
Count: {count}
</button>
<script>
let count = 0
</script>
<button on:click={() => count += 1}>
Count: {count}
</button>
Events
15
<form on:submit|preventDefault={handleSubmit}>
<!-- the `submit` event's default is prevented,
so the page won't reload -->
</form>
Available modifiers:
preventDefault,stopPropagation,capture,once
Component-centred approach
16
<script>
import BlogPost from
'./BlogPost.svelte'
const postTitle = 'Svelte rocks'
</script>
<BlogPost title={postTitle}/>
<script>
export let title = 'Default title'
</script>
<h1>
{title}
</h1>
Lifecycle
17
<script>
import { onMount } from 'svelte'
onMount(() => {
const interval = setInterval(() => {
console.log('beep')
}, 1000)
return () => clearInterval(interval)
})
</script>
Component lifecycle hooks:
onMount,beforeUpdate,afterUpdate,onDestroy,tick
Demo time
03
● Libs ecosystem
● Community and learning materials
● Typescript support
//TODO
19
● REPL: https://svelte.dev/repl/hello-world?version=3.16.0
● Sapper (SSR like Next.js/Nuxt): https://sapper.svelte.dev/
● Works on mobile with NativeScript
Advanced
20
Links
● Docs: svelte.dev/
● Discord chat: svelte.dev/chat
● Amsterdam Svelte Meetup - meetup.com/Amsterdam-SvelteJS/
● JSDay 2019 slides
● JSCAMP 2019 - The Return of 'Write Less, Do More
21
Thanks! Questions?

Svelte JS introduction