Component Format
Script
Components are the building blocks of Svelte applications. They are written into .svelte
files, using a superset of HTML.
All three sections — script, styles and markup — are optional.
<script>
A <script>
block contains JavaScript that runs when a component instance is created. Variables declared (or imported) at the top level are 'visible' from the component's markup. There are four additional rules:
export
creates a component prop
1. Svelte uses the export
keyword to mark a variable declaration as a property or prop, which means it becomes accessible to consumers of the component (see the section on attributes and props for more information).
You can specify a default initial value for a prop. It will be used if the component's consumer doesn't specify the prop on the component (or if its initial value is undefined
) when instantiating the component. Note that whenever a prop is removed by the consumer, its value is set to undefined
rather than the initial value.
In development mode (see the compiler options), a warning will be printed if no default initial value is provided and the consumer does not specify a value. To squelch this warning, ensure that a default initial value is specified, even if it is undefined
.
If you export a const
, class
or function
, it is readonly from outside the component. Function expressions are valid props, however.
Readonly props can be accessed as properties on the element, tied to the component using bind:this
syntax.
You can use reserved words as prop names.
2. Assignments are 'reactive'
To change component state and trigger a re-render, just assign to a locally declared variable.
Update expressions (count += 1
) and property assignments (obj.x = y
) have the same effect.
Because Svelte's reactivity is based on assignments, using array methods like .push()
and .splice()
won't automatically trigger updates. A subsequent assignment is required to trigger the update. This and more details can also be found in the tutorial.
$:
marks a statement as reactive
3. Any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it with the $:
JS label syntax. Reactive statements run immediately before the component updates, whenever the values that they depend on have changed.
Only values which directly appear within the $:
block will become dependencies of the reactive statement. For example, in the code below total
will only update when x
changes, but not y
.
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a let
declaration on your behalf.
$
to access their values
4. Prefix stores with A store is an object that allows reactive access to a value via a simple store contract. The svelte/store
module contains minimal store implementations which fulfil this contract.
Any time you have a reference to a store, you can access its value inside a component by prefixing it with the $
character. This causes Svelte to declare the prefixed variable, subscribe to the store at component initialization and unsubscribe when appropriate.
Assignments to $
-prefixed variables require that the variable be a writable store, and will result in a call to the store's .set
method.
Note that the store must be declared at the top level of the component — not inside an if
block or a function, for example.
Local variables (that do not represent store values) must not have a $
prefix.
Store contract
You can create your own stores without relying on svelte/store
, by implementing the store contract:
- A store must contain a
.subscribe
method, which must accept as its argument a subscription function. This subscription function must be immediately and synchronously called with the store's current value upon calling.subscribe
. All of a store's active subscription functions must later be synchronously called whenever the store's value changes. - The
.subscribe
method must return an unsubscribe function. Calling an unsubscribe function must stop its subscription, and its corresponding subscription function must not be called again by the store. - A store may optionally contain a
.set
method, which must accept as its argument a new value for the store, and which synchronously calls all of the store's active subscription functions. Such a store is called a writable store.
For interoperability with RxJS Observables, the .subscribe
method is also allowed to return an object with an .unsubscribe
method, rather than return the unsubscription function directly. Note however that unless .subscribe
synchronously calls the subscription (which is not required by the Observable spec), Svelte will see the value of the store as undefined
until it does.