## Svelte cheatsheet
[Doc examples](https://svelte.dev/examples)
### Table of Contents
1. [Structure](#structure)
2. [Imports](#imports)
3. [Variables](#variables)
4. [Reactivity](#reactivity)
1. [Variables](#variables)
2. [Statements](#statements)
3. [Updating objects](#updating-objects)
5. [Props](#props)
6. [Logic](#logic)
1. [Conditions](#conditions)
2. [Loops](#loops)
3. [Promises](#promises)
7. [Events](#events)
1. [Event dispatcher](#event-dispatcher)
2. [Event forwarding](#event-forwarding)
8. [Bindings](#bindings)
9. [Lifecycle](#lifecycle)
10. [Store](#store)
1. [Usage](#usage)
2. [Readable store](#readable-store)
3. [Writable store](#writable-store)
4. [Derived store](#derived-store)
5. [Custom store](#custom-store)
11. [Motion](#motion)
1. [Tweened](#tweened)
2. [Spring](#spring)
12. [Transitions](#transitions)
13. [Dynamic classes](#dynamic-classes)
14. [Slots](#slots)
15. [Contexts](#contexts)
16. [Debugging](#debugging)
17. [Integrations](#integrations)
1. [GraphQL integration](#graphql-integration)
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:
`