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/01-introduction/04-svelte-js-files.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ title: .svelte.js and .svelte.ts files
5
5
6
6
Besides `.svelte` files, Svelte also operates on `.svelte.js` and `.svelte.ts` files.
7
7
8
-
These behave like any other `.js` or `.ts` module, except that you can use runes. This is useful for creating reusable reactive logic, or sharing reactive state across your app.
8
+
These behave like any other `.js` or `.ts` module, except that you can use runes. This is useful for creating reusable reactive logic, or sharing reactive state across your app (though note that you [cannot export reassigned state]($state#Passing-state-across-modules)).
9
9
10
10
> [!LEGACY]
11
11
> This is a concept that didn't exist prior to Svelte 5
...though if you find yourself writing code like that, consider using [classes](#Classes) instead.
254
+
255
+
## Passing state across modules
256
+
257
+
You can declare state in `.svelte.js` and `.svelte.ts` files, but you can only _export_ that state if it's not directly reassigned. In other words you can't do this:
258
+
259
+
```js
260
+
/// file: state.svelte.js
261
+
exportlet count =$state(0);
262
+
263
+
exportfunctionincrement() {
264
+
count +=1;
265
+
}
266
+
```
267
+
268
+
That's because every reference to `count` is transformed by the Svelte compiler — the code above is roughly equivalent to this:
269
+
270
+
```js
271
+
/// file: state.svelte.js (compiler output)
272
+
// @filename: index.ts
273
+
interface Signal<T> {
274
+
value:T;
275
+
}
276
+
277
+
interface Svelte {
278
+
state<T>(value?:T): Signal<T>;
279
+
get<T>(source: Signal<T>):T;
280
+
set<T>(source: Signal<T>, value:T):void;
281
+
}
282
+
declare const $:Svelte;
283
+
// ---cut---
284
+
exportlet count =$.state(0);
285
+
286
+
exportfunctionincrement() {
287
+
$.set(count, $.get(count) +1);
288
+
}
289
+
```
290
+
291
+
> [!NOTE] You can see the code Svelte generates by clicking the 'JS Output' tab in the [playground](/playground).
292
+
293
+
Since the compiler only operates on one file at a time, if another file imports `count` Svelte doesn't know that it needs to wrap each reference in `$.get` and `$.set`:
294
+
295
+
```js
296
+
// @filename: state.svelte.js
297
+
exportlet count =0;
298
+
299
+
// @filename: index.js
300
+
// ---cut---
301
+
import { count } from'./state.svelte.js';
302
+
303
+
console.log(typeof count); // 'object', not 'number'
304
+
```
305
+
306
+
This leaves you with two options for sharing state between modules — either don't reassign it...
When Svelte runs an effect function, it tracks which pieces of state (and derived state) are accessed (unless accessed inside [`untrack`](svelte#untrack)), and re-runs the function when that state later changes.
@@ -136,19 +136,33 @@ An effect only reruns when the object it reads changes, not when a property insi
136
136
137
137
An effect only depends on the values that it read the last time it ran. This has interesting implications for effects that have conditional code.
138
138
139
-
For instance, if `a` is `true` in the code snippet below, the code inside the `if` block will run and `b` will be evaluated. As such, changes to either `a` or `b`[will cause the effect to re-run](/playground/untitled#H4sIAAAAAAAAE3VQzWrDMAx-FdUU4kBp71li6EPstOxge0ox8-QQK2PD-N1nLy2F0Z2Evj9_chKkP1B04pnYscc3cRCT8xhF95IEf8-Vq0DBr8rzPB_jJ3qumNERH-E2ECNxiRF9tIubWY00lgcYNAywj6wZJS8rtk83wjwgCrXHaULLUrYwKEgVGrnkx-Dx6MNFNstK5OjSbFGbwE0gdXuT_zGYrjmAuco515Hr1p_uXak3K3MgCGS9s-9D2grU-judlQYXIencnzad-tdR79qZrMyvw9wd5Z8Yv1h09dz8mn8AkM7Pfo0BAAA=).
139
+
For instance, if `condition` is `true` in the code snippet below, the code inside the `if` block will run and `color` will be evaluated. As such, changes to either `condition` or `color`[will cause the effect to re-run](/playground/untitled#H4sIAAAAAAAAE21RQW6DMBD8ytaNBJHaJFLViwNIVZ8RcnBgXVk1xsILTYT4e20TQg89IOPZ2fHM7siMaJBx9tmaWpFqjQNlAKXEihx7YVJpdIyfRkY3G4gB8Pi97cPanRtQU8AuwuF_eNUaQuPlOMtc1SlLRWlKUo1tOwJflUikQHZtA0klzCDc64Imx0ANn8bInV1CDhtHgjClrsftcSXotluLybOUb3g4JJHhOZs5WZpuIS9gjNqkJKQP5e2ClrR4SMdZ13E4xZ8zTPOTJU2A2uE_PQ9COCI926_hTVarIU4hu_REPlBrKq2q73ycrf1N-vS4TMUsulaVg3EtR8H9rFgsg8uUsT1B2F9eshigZHBRpuaD0D3mY8Qm2BfB5N2YyRzdNEYVDy0Ja-WsFjcOUuP1HvFLWA6H3XuHTUSmmDV2--0TXonxsKbp7G9C6R__NONS-MFNvxj_d6mBAgAA).
140
140
141
-
Conversely, if `a` is `false`, `b` will not be evaluated, and the effect will _only_ re-run when `a` changes.
141
+
Conversely, if `condition` is `false`, `color` will not be evaluated, and the effect will _only_ re-run again when `condition` changes.
142
142
143
143
```ts
144
-
let a =false;
145
-
let b =false;
144
+
// @filename: ambient.d.ts
145
+
declaremodule'canvas-confetti' {
146
+
interfaceConfettiOptions {
147
+
colors:string[];
148
+
}
149
+
150
+
function confetti(opts?:ConfettiOptions):void;
151
+
exportdefaultconfetti;
152
+
}
153
+
154
+
// @filename: index.js
146
155
// ---cut---
147
-
$effect(() => {
148
-
console.log('running');
156
+
importconfettifrom'canvas-confetti';
149
157
150
-
if (a) {
151
-
console.log('b:', b);
158
+
let condition =$state(true);
159
+
let color =$state('#ff3e00');
160
+
161
+
$effect(() => {
162
+
if (condition) {
163
+
confetti({ colors: [color] });
164
+
} else {
165
+
confetti();
152
166
}
153
167
});
154
168
```
@@ -212,20 +226,19 @@ It is used to implement abstractions like [`createSubscriber`](/docs/svelte/svel
212
226
213
227
The `$effect.root` rune is an advanced feature that creates a non-tracked scope that doesn't auto-cleanup. This is useful for nested effects that you want to manually control. This rune also allows for the creation of effects outside of the component initialisation phase.
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/svelte/03-template-syntax/13-transition.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,10 +23,6 @@ The `transition:` directive indicates a _bidirectional_ transition, which means
23
23
{/if}
24
24
```
25
25
26
-
## Built-in transitions
27
-
28
-
A selection of built-in transitions can be imported from the [`svelte/transition`](svelte-transition) module.
29
-
30
26
## Local vs global
31
27
32
28
Transitions are local by default. Local transitions only play when the block they belong to is created or destroyed, _not_ when parent blocks are created or destroyed.
@@ -41,6 +37,10 @@ Transitions are local by default. Local transitions only play when the block the
41
37
{/if}
42
38
```
43
39
40
+
## Built-in transitions
41
+
42
+
A selection of built-in transitions can be imported from the [`svelte/transition`](svelte-transition) module.
0 commit comments