Skip to content

Commit 1a341a2

Browse files
authored
docs: rename /docs/functions to /docs/imports, include svelte/reactivity, tweak various things (#11187)
1 parent 30fa876 commit 1a341a2

File tree

4 files changed

+114
-87
lines changed

4 files changed

+114
-87
lines changed

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,6 @@ This can improve performance with large arrays and objects that you weren't plan
9393

9494
> Objects and arrays passed to `$state.frozen` will be shallowly frozen using `Object.freeze()`. If you don't want this, pass in a clone of the object or array instead.
9595
96-
### Reactive Map, Set and Date
97-
98-
Svelte provides reactive `Map`, `Set` and `Date` classes. These can be imported from `svelte/reactivity` and used just like their native counterparts.
99-
100-
```svelte
101-
<script>
102-
import { Map } from 'svelte/reactivity';
103-
104-
const map = new Map();
105-
map.set('message', 'hello');
106-
107-
function update_message() {
108-
map.set('message', 'goodbye');
109-
}
110-
</script>
111-
112-
<p>{map.get('message')}</p>
113-
```
114-
11596
## `$state.snapshot`
11697

11798
To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:

sites/svelte-5-preview/src/routes/docs/content/01-api/05-functions.md

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { redirect } from '@sveltejs/kit';
2+
3+
export function load() {
4+
redirect(308, '/docs/imports');
5+
}

0 commit comments

Comments
 (0)