|
| 1 | +--- |
| 2 | +title: Imports |
| 3 | +--- |
| 4 | + |
| 5 | +As well as runes, Svelte 5 introduces a handful of new things you can import, alongside existing ones like `getContext`, `setContext` and `tick`. |
| 6 | + |
| 7 | +## `svelte` |
| 8 | + |
| 9 | +### `mount` |
| 10 | + |
| 11 | +Instantiates a component and mounts it to the given target: |
| 12 | + |
| 13 | +```js |
| 14 | +// @errors: 2322 |
| 15 | +import { mount } from 'svelte'; |
| 16 | +import App from './App.svelte'; |
| 17 | + |
| 18 | +const app = mount(App, { |
| 19 | + target: document.querySelector('#app'), |
| 20 | + props: { some: 'property' } |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +### `hydrate` |
| 25 | + |
| 26 | +Like `mount`, but will reuse up any HTML rendered by Svelte's SSR output (from the [`render`](#svelte-server-render) function) inside the target and make it interactive: |
| 27 | + |
| 28 | +```js |
| 29 | +// @errors: 2322 |
| 30 | +import { hydrate } from 'svelte'; |
| 31 | +import App from './App.svelte'; |
| 32 | + |
| 33 | +const app = hydrate(App, { |
| 34 | + target: document.querySelector('#app'), |
| 35 | + props: { some: 'property' } |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +### `unmount` |
| 40 | + |
| 41 | +Unmounts a component created with [`mount`](#svelte-mount) or [`hydrate`](#svelte-hydrate): |
| 42 | + |
| 43 | +```js |
| 44 | +// @errors: 1109 |
| 45 | +import { mount, unmount } from 'svelte'; |
| 46 | +import App from './App.svelte'; |
| 47 | + |
| 48 | +const app = mount(App, {...}); |
| 49 | + |
| 50 | +// later |
| 51 | +unmount(app); |
| 52 | +``` |
| 53 | + |
| 54 | +### `untrack` |
| 55 | + |
| 56 | +To prevent something from being treated as an `$effect`/`$derived` dependency, use `untrack`: |
| 57 | + |
| 58 | +```svelte |
| 59 | +<script> |
| 60 | + import { untrack } from 'svelte'; |
| 61 | +
|
| 62 | + let { a, b } = $props(); |
| 63 | +
|
| 64 | + $effect(() => { |
| 65 | + // this will run when `a` changes, |
| 66 | + // but not when `b` changes |
| 67 | + console.log(a); |
| 68 | + console.log(untrack(() => b)); |
| 69 | + }); |
| 70 | +</script> |
| 71 | +``` |
| 72 | + |
| 73 | +## `svelte/reactivity` |
| 74 | + |
| 75 | +Svelte provides reactive `Map`, `Set`, `Date` and `URL` classes. These can be imported from `svelte/reactivity` and used just like their native counterparts. [Demo:](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE32QzWrDMBCEX2Wri1uo7bvrBHrvqdBTUogqryuBfhZp5SQYv3slSsmpOc7uN8zsrmI2FpMYDqvw0qEYxCuReBZ8pSrSgpax6BRyVHUyJhUN8f7oj2wchciwwsf7G2wwx-Cg-bX0EaVisxi-Ni-FLbQKPjHkaGEHHs_V9NhoZkpD3-NFOrLYqeB6kqybp-Ia-1uYHx_aFpSW_hsTcADWmLDrOmjbsh-Np8zwZfw0LNJm3K0lqaMYOKhgt_8RHRLX0-8gtdAfUiAdb4XOxlrINElGOOmI8wmkn2AxCmHBmOTdetWw7ct7XZjMbHASA8eM2-f2A-JarmyZAQAA) |
| 76 | + |
| 77 | +```svelte |
| 78 | +<script> |
| 79 | + import { URL } from 'svelte/reactivity'; |
| 80 | +
|
| 81 | + const url = new URL('https://example.com/path'); |
| 82 | +</script> |
| 83 | +
|
| 84 | +<!-- changes to these... --> |
| 85 | +<input bind:value={url.protocol} /> |
| 86 | +<input bind:value={url.hostname} /> |
| 87 | +<input bind:value={url.pathname} /> |
| 88 | +
|
| 89 | +<hr /> |
| 90 | +
|
| 91 | +<!-- will update `href` and vice versa --> |
| 92 | +<input bind:value={url.href} /> |
| 93 | +``` |
| 94 | + |
| 95 | +## `svelte/server` |
| 96 | + |
| 97 | +### `render` |
| 98 | + |
| 99 | +Only available on the server and when compiling with the `server` option. Takes a component and returns an object with `html` and `head` properties on it, which you can use to populate the HTML when server-rendering your app: |
| 100 | + |
| 101 | +```js |
| 102 | +// @errors: 2724 2305 2307 |
| 103 | +import { render } from 'svelte/server'; |
| 104 | +import App from './App.svelte'; |
| 105 | + |
| 106 | +const result = render(App, { |
| 107 | + props: { some: 'property' } |
| 108 | +}); |
| 109 | +``` |
0 commit comments