--- title: Component format --- --- 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. ```html ``` ### <script> A ` ``` --- You can specify a default value, which will be used if the component's consumer doesn't specify a prop. In development mode (see the [compiler options](docs#svelte_compile)), a warning will be printed if no default is provided and the consumer does not specify a value. To squelch this warning, ensure that a default is specified, even if it is `undefined`. ```html ``` --- If you export a `const`, `class` or `function`, it is readonly from outside the component. Function *expressions* are valid props, however. ```html ``` --- You can use reserved words as prop names. ```html ``` ##### 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. Options for getting around this can be found in the [tutorial](tutorial/updating-arrays-and-objects). ```html ``` ##### 3. `$:` marks a statement as reactive --- 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label). Reactive statements run immediately before the component updates, whenever the values that they depend on have changed. ```html ``` --- If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf. ```html ``` ##### 4. Prefix stores with `$` to access their values --- A *store* is any object that allows reactive access to a value via a simple *store contract*. The [`svelte/store` module](docs#svelte_store) contains minimal store implementations which fulfil this contract. You can use these as the basis for your own stores, or you can implement your stores from scratch. 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 also return an unsubscription function. Calling an unsubscription 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*. ```js const unsubscribe = store.subscribe(value => { console.log(value); }); // logs `value` // later... unsubscribe(); ``` --- 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, and set up a store subscription that will be unsubscribed 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. ```html ``` ### <script context="module"> --- A ` ``` ### <style> --- CSS inside a ` ``` --- To apply styles to a selector globally, use the `:global(...)` modifier. ```html ```