## 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}
  • {i}: {cat.name}
  • {/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)
  • {i}: {cat.name}
  • {/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: ` ``` ### Motion Stores are cool but you can make them even smoother by adding animation when transposed to UI. #### Tweened It's usable like an ordinary store: ```svelte ``` #### Spring (works better with frequently changing value) ```svelte ``` ### Transitions There're a lot: - Fade in/out: `transition:fade` (ie: `

    Some text

    `) - Fly (disappear in a given direction): `transition:fly` - ... ### Dynamic classes To affect a `class` to a tag dynamically when condition are met: ```javascript ``` Here we affect the class `.active` when condition is true. ### Slots Slots give you the opportunity to shape a component inside another one. ```svelte

    Hello!

    This is a box. It can contain anything.

    // Box.svelte
    A default value here
    ``` Slots can be named: ```svelte T. Pellegatta // ContactCard.svelte

    Unknown name // Will display T. Pellegatta because it has been defined

    Unknown address
    ``` You can pass props to slots: ```svelte
    {item.text}

    Copyright (c) 2019 Svelte Industries

      {#each items as item}
    • {/each}
    ``` The `let:` directive goes on the element with the slot attribute. ### Contexts Almost the same as **stores** but **less reactive**. [Documentation](https://svelte.dev/docs#setContext) ### Debugging Instead of using `debugger;` use `{@debug variable}` ## Integrations ### GraphQL integration (with Apollo) You'll have to import some plugins: - `apollo-boost` - `graphql` - `svelte-apollo` Then, in the main component instantiate a client: ```svelte ``` In another one (or the same) use it to fetch/push data: ```svelte {#await $todos} Loading... {:then result}

    Total todos: {result.data.getTodos.length}

      {each result.data.getTodos as todo}
    • {todo.text}
    • {/each}
    {:catch error}

    {error.message}

    {/await} ```