I'm not a expert.
So, please, if I make any mistake, just tell me so!
I'm just a Svelte lover and I want to spread the word...
(() => {
const square = number => number * number;
const secondNumber = square(firstNumber);
const firstNumber = 42;
console.log(secondNumber);
})();
$: foo = bar
// in vanilla js
let foo = 10
let bar = foo + 10 // bar = 20
foo = bar // bar = 20 no reactivity
bar = foo + 10 // now bar becomes 25
// in Svelte
let foo = 10
$: bar = foo + 10 // now bar is 20
foo = 15
// bar is now 25 because it is bound to the value of foo
What year were you born?
{#if age}
You are {age} years old
{/if}
13 lines of codes
import React, {Component} from 'react'
export default () => {
const {age, setAge} = useState(0)
const {year, setYear} = useState(0)
function calculateAge(){
const currentYear = new Date().getFullYear()
setAge(currentYear - year)
}
function handleChange(event){
setYear(event.target.value)
}
return(
What year were you born?
{age ? You are {age} years old
: null}
)}
25 lines of code
Svelte compiles your code into low-level, pure javascript that will interact directly with the DOM
npx degit sveltejs/template my-svelte-project
cd my-svelte-project
npm install
npm run dev
You write your code in a .svelte file
Hello {name}!
// main.js
import App from './App.svelte';
const app = new App({
target: document.body
});
export default app;
// main.js
import App from './App.svelte';
import App2 from './App2.svelte';
const app = new App({
target: document.getElementById('div1')
});
const app2 = new App({
target: document.getElementById('div2')
});
export default app;
Scope styles
// App.svelte
These styles...
// Nested.svelte
{name}
This is reactive variable {foo}
{#if condition} {:else if} {:else} {/if}
{#if x >= 10}
{x} is greater than 10
{:else if x > 5}
{x} is greater than 10
{:else}
{#if 5 > x}
{x} is less than 5
{:else}
{x} is between 5 and 10
{/if}
{/if}
{#each yourArray as yourSingleElement} {/each}
The Famous Cats of YouTube
{#each cats as cat}
-
{cat.name}
{/each}
on:message={thenameyouwant}
bind:value={yourValue}
{a} + {b} = {a + b}
In a theme or a plugin