You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/svelte/02-runes/03-$derived.md
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,3 +52,20 @@ In essence, `$derived(expression)` is equivalent to `$derived.by(() => expressio
52
52
Anything read synchronously inside the `$derived` expression (or `$derived.by` function body) is considered a _dependency_ of the derived state. When the state changes, the derived will be marked as _dirty_ and recalculated when it is next read.
53
53
54
54
To exempt a piece of state from being treated as a dependency, use [`untrack`](svelte#untrack).
55
+
56
+
## Update propagation
57
+
58
+
Svelte uses something called _push-pull reactivity_ — when state is updated, everything that depends on the state (whether directly or indirectly) is immediately notified of the change (the 'push'), but derived values are not re-evaluated until they are actually read (the 'pull').
59
+
60
+
If the new value of a derived is referentially identical to its previous value, downstream updates will be skipped. In other words, Svelte will only update the text inside the button when `large` changes, not when `count` changes, even though `large` depends on `count`:
It is possible to use runes inside your test files. First ensure your bundler knows to route the file through the Svelte compiler before running the test by adding `.svelte` to the filename (e.g `multiplier.svelte.test.js`). After that, you can use runes inside your tests.
80
+
Since Vitest processes your test files the same way as your source files, you can use runes inside your tests as long as the filename includes `.svelte`:
60
81
61
82
```js
62
83
/// file: multiplier.svelte.test.js
@@ -76,6 +97,21 @@ test('Multiplier', () => {
76
97
});
77
98
```
78
99
100
+
```js
101
+
/// file: multiplier.svelte.js
102
+
/**
103
+
* @param{() => number}getCount
104
+
* @param{number}k
105
+
*/
106
+
exportfunctionmultiplier(getCount, k) {
107
+
return {
108
+
getvalue() {
109
+
returngetCount() * k;
110
+
}
111
+
};
112
+
}
113
+
```
114
+
79
115
If the code being tested uses effects, you need to wrap the test inside `$effect.root`:
80
116
81
117
```js
@@ -106,6 +142,27 @@ test('Effect', () => {
106
142
});
107
143
```
108
144
145
+
```js
146
+
/// file: logger.svelte.js
147
+
/**
148
+
* @param{() => any}getValue
149
+
*/
150
+
exportfunctionlogger(getValue) {
151
+
/**@type{any[]}*/
152
+
let log =$state([]);
153
+
154
+
$effect(() => {
155
+
log.push(getValue());
156
+
});
157
+
158
+
return {
159
+
getvalue() {
160
+
return log;
161
+
}
162
+
};
163
+
}
164
+
```
165
+
109
166
### Component testing
110
167
111
168
It is possible to test your components in isolation using Vitest.
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/svelte/07-misc/04-custom-elements.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -126,3 +126,4 @@ Custom elements can be a useful way to package components for consumption in a n
126
126
- The deprecated `let:` directive has no effect, because custom elements do not have a way to pass data to the parent component that fills the slot
127
127
- Polyfills are required to support older browsers
128
128
- You can use Svelte's context feature between regular Svelte components within a custom element, but you can't use them across custom elements. In other words, you can't use `setContext` on a parent custom element and read that with `getContext` in a child custom element.
129
+
- Don't declare properties or attributes starting with `on`, as their usage will be interpreted as an event listener. In other words, Svelte treats `<custom-element oneworld={true}></custom-element>` as `customElement.addEventListener('eworld', true)` (and not as `customElement.oneworld = true`)
0 commit comments