Svelte components are defined in .svelte
files, containing HTML, CSS, and JavaScript.
<!-- MyComponent.svelte -->
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
<style>
h1 {
color: blue;
}
</style>
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 components are defined in
|
Key Features:
|
|
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
|
|
Example:
|
Svelte uses
|
Use
|
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
|
|
Example:
|
|
Apply transitions when elements enter or leave the DOM.
|
|
Apply different transitions for entering and leaving.
|
Built-in transitions |
|
|
Animate changes to numeric values.
|
Custom animations |
Use the |
Stores are a way to manage state outside of components.
|
Actions |
Actions are functions that run when an element is created and can return an object with a
|
Using Actions |
|
|