## Svelte cheatsheet
[Doc examples](https://svelte.dev/examples)
Your components are inside `.svelte` files.
### Structure
Your component use few tags to be operating:
- `
```
### Variables
You can define variables in your `
Some text
```
> **Tip**: It also works inside double-quotes: `
Some text
`
Moreover, when var = attribute, (`src={src}`) you can use it this way: `{src}`.
### Reactivity
**Reactivity is triggered by assignments**
What does it mean? Every change will be triggered after an assignment has been done.
More on that later.
##### Variables
Sometimes a variable is result from a computation, you'll have to use a *reactive declaration*:
```javascript
let count = 0
$: doubled = count * 2
```
##### Statements
We're not limited to variable declaration, we can also use reactive statements:
```javascript
$: console.log(`the count is ${count}`);
```
Here `console.log` is called whenever `count` value changes.
Notice the usage of *`* (backquotes).
> **Tip**: You can group more statements by using `$: { ... }`.
##### Updating objects
Because Svelte's reactivity is **triggered** by **assignments** following code won't do anything when called:
```javascript
function addNumber() {
numbers.push(numbers.length + 1);
}
```
Unless you add it this assignment:
```javascript
function addNumber() {
numbers.push(numbers.length + 1);
numbers = numbers;
}
```
### Props
There's an easy way to pass props to a child component. Just `export` it !
```svelte
// Nested.svelte
The answer is {answer}
// App.svelte
// Will display: The answer is 42
```
> **Tip**: You can also use `` to set a default value to a prop that hasn't been initialized into it's own component.
When passing multiple **props** typically as an object of properties, you can **spread** them to a component instead of specifying each one with `...`:
```svelte
```
Assuming we have `export`ed `name`, `version`, and so on in the `Info` component.
### Logic
#### Conditions
To conditionally render some markup, we wrap it in an if block:
```svelte
{#if user.loggedIn}
{/if}
{#if !user.loggedIn}
{/if}
```
You can also make use of :
- `{:else}`
- `{:else if .. }`
#### Loops
Expressed this way:
```svelte
{#each cats as cat, i}
{/each}
```
> **Tip**: You can also de-structure as: `{#each cats as { id, name }}` and then use `{name}` instead of `{cat.name}`
When a value isn't **exported** but is computed by a **prop**, you can go into problems when updating values since it's a *copy* and not a *reference*.
Thus, be sure to **identify** the right object when updating its value(s):
```svelte
{#each cats as cat, i (cat)} // Notice the (cat)
{/each}
```
#### Promises
You can deal with **promises** and display at each of the promise lifecycle:
```svelte
{#await promise}
Loading...
{:then result}
It returns {result}
{:catch error}
An error occurred: {error.message}
{/await}
```
> **Tip**: You can also skip loading step by using: `{#await promise then result}`
### Events
You can bind functions / events to your tags:
```svelte
```
You can use all javascript event (click, mousemove, ...).
You can also *modify* event by *piping* **modifiers** to it:
`