What is it?
Svelte is a component framework that uses a "imperative" code that updates the DOM directly.
Performance
Svelte is extremely fast! I know that some frameworks have the called "live-reload", that is the feature where you modify the code and soon the changes will be reflected in the development server. Svelte has the same... or I would call it "live-reloaded" as it is faster than blinking your eyes hahahah.
Of course, great part of this power comes from the fact that it uses non-transpiled code and pure Javascript.
Ease of learning
.svelte type. (kind of HTML + CSS + JS)Who uses Svelte
Hands-on
As a first Svelte training, let’s create a super Svelte stepper to hold a number for us.
Steps:
- Template clone
- Clone the svelte app template: https://github.com/sveltejs/template
- Hello World - See it running
- Run
yarn installto download the dependencies; - Run
yarn devto see the template running; - Open
http://localhost:5000in your browser; - Modify the
nameprop insrc/main.jswith any string and take another look in the browser... Voilà! - First Component
- Create component
- Create a new file, inside
/src, calledStepper.svelte(you could copyApp.svelte.
Note that there are 3 “sections”:
<!-- JavaScript code -->
<script></script>
<!-- CSS code -->
<style></style>
<!-- HTML (template) elements code -->
<main></main> - Inside the
let create a variable:
export let value;export.Also, declare a variable inside App component:
let stepperValue = 0;- Inside the
tag let put some elements to see and manipulate this variable:
<button>-</button><h1>{value}</h1><button>+</button>App - : import Stepper from './Stepper.svelte'; <Stepper value={stepperValue}></Stepper>stepperValue value from App. tag: main {
display: flex;
justify-content: center;
align-items: center
}
button {
border: 1px gray solid;
height: 40px;
width: 40px;
border-radius: 25%;
font-weight: bolder;
margin: 5px;
}
Great! Now we can see our very useful stepper. The next step is make it functional and change the value number.
createEventDispatcher function from svelte package and create our custom event: import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
const handleClick = (btnValue)=>{
dispatch('btnValue', {
value: btnValue
});
}So, to see the reactivity of Svelte in action, set the event handler “on:click” inside the buttons element. :
<button on:click={() => handleClick('-')}>-</button>
<h1>{value}</h1>
<button on:click={() => handleClick('+')}>+</button>App component: function handleStepperEvents(event){
console.log(event);
const value = event.detail.value;
if(value === '-'){
stepperValue--;
}else {
stepperValue++;
}
}
And set this handler for the Stepper element:
<Stepper on:btnValue={handleStepperEvents} value={stepperValue}></Stepper>When Stepper emits the btnValue event, handleStepperEvents function will be executed.
It’s already working!!!
Let’s try one more trick.
Inside App, create a variable like this:
$: isEven = stepperValue % 2 == 0;The idea of the Dolar Sign ($) is that Svelte interprets it as re-run this code whenever any of the referenced values changes.
element: {#if isEven}
<h1>{stepperValue} is EVEN!</h1>
{:else}
<h1>{stepperValue} is ODD!</h1>
{/if}Conclusion
Svelte has a lot of good practices that make our development process faster and more enjoyable in some ways but as this "frameworks" is kind of new, probably, will be harder to find discussions about problems and pre-made libs to use. Maybe I didn't got problems with my project because it was too small and I would confront real world problems in bigger projects.
In my opinion, Svelte is a good shot if you are planning to do a personal project or you have a small and "closed scope" project at work. So, you could have the possibility to test and take the conclusions by yourself.
If you didn't tried yet, don't waste your time and go for it now! Remember that, every knowledge you get from an areas will make you better in others.
https://svelte.dev/tutorial/basics