Skip to content

Commit 24151c4

Browse files
authored
docs: document event delegation and effect deferral (#11747)
* docs: document event delegation and effect deferral * document flushSync
1 parent 8459098 commit 24151c4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ As well as runes, Svelte 5 introduces a handful of new things you can import, al
66

77
## `svelte`
88

9+
### `flushSync`
10+
11+
Forces any pending effects (including DOM updates) to be applied immediately, rather than in the future. This is mainly useful in a testing context — you'll rarely need it in application code.
12+
13+
```svelte
14+
<script>
15+
import { flushSync } from 'svelte';
16+
17+
let count = $state(0);
18+
let element;
19+
20+
function onclick() {
21+
flushSync(() => (count += 1));
22+
23+
// without `flushSync`, the DOM would be updated in the future
24+
console.log(element.textContent === String(count));
25+
}
26+
</script>
27+
28+
<span bind:this={element}>{count}</span>
29+
<button {onclick}>update</button>
30+
```
31+
932
### `mount`
1033

1134
Instantiates a component and mounts it to the given target:

sites/svelte-5-preview/src/routes/docs/content/03-appendix/02-breaking-changes.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ In Svelte 4, doing the following triggered reactivity:
152152

153153
This is because the Svelte compiler treated the assignment to `foo.value` as an instruction to update anything that referenced `foo`. In Svelte 5, reactivity is determined at runtime rather than compile time, so you should define `value` as a reactive `$state` field on the `Foo` class. Wrapping `new Foo()` with `$state(...)` will have no effect — only vanilla objects and arrays are made deeply reactive.
154154

155+
### Events and bindings use delegated handlers
156+
157+
Event handlers added to elements with things like `onclick={...}` or `bind:value={...}` use _delegated_ handlers where possible. This means that Svelte attaches a single event handler to a root node rather than using `addEventListener` on each individual node, resulting in better performance and memory usage.
158+
159+
Additionally, updates that happen inside event handlers will not be reflected in the DOM (or in `$effect(...)`) until the browser has had a chance to repaint. This ensures that your app stays responsive, and your Core Web Vitals (CWV) aren't penalised.
160+
161+
In very rare cases you may need to force updates to happen immediately. You can do this with `flushSync`:
162+
163+
```diff
164+
<script>
165+
+ import { flushSync } from 'svelte';
166+
167+
function onclick() {
168+
- update_something();
169+
+ flushSync(() => update_something());
170+
}
171+
</script>
172+
```
173+
155174
## Other breaking changes
156175

157176
### Stricter `@const` assignment validation

0 commit comments

Comments
 (0)