Catalog / Svelte Cheatsheet
Svelte Cheatsheet
A concise reference for Svelte syntax, directives, lifecycle methods, and best practices. This cheat sheet provides a quick guide to building reactive web applications with Svelte.
Svelte Basics
Component Syntax
Svelte components are defined in
|
Key Features:
|
Reactivity
|
Declares a reactive variable. Svelte tracks assignments to these variables and updates the DOM accordingly.
|
|
Re-executes the code block whenever any of the referenced variables change.
|
Updating Arrays and Objects |
Arrays and objects require special handling to trigger reactivity. Use methods like
|
Basic Directives
|
Example:
|
Control Flow & Components
Conditional Rendering
Svelte uses
|
List Rendering
Use
|
Component Communication
Props |
Pass data from parent to child components using props.
|
Events |
Dispatch custom events from child to parent components.
|
Context API |
Share data between components without explicitly passing props through every level. Use
|
Lifecycle and Advanced Features
Lifecycle Methods
|
Example:
|
Transitions
|
Apply transitions when elements enter or leave the DOM.
|
|
Apply different transitions for entering and leaving.
|
Built-in transitions |
|
Animations
|
Animate changes to numeric values.
|
Custom animations |
Use the |
Stores and Actions
Stores
Stores are a way to manage state outside of components.
|
Actions
Actions |
Actions are functions that run when an element is created and can return an object with a
|
Using Actions |
|
Advanced Directives
|