Skip to content

Commit 4fe68e9

Browse files
authored
Tutorial part two (#443)
* move transitions/animations into part one * update a bunch of tutorial exercises * move deferred transitions/animations back into part two * make room * add section on inspecting state * add raw state exercise * fix * reactive classes * more class stuff * built-ins
1 parent 4e0f083 commit 4fe68e9

File tree

70 files changed

+718
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+718
-35
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let numbers = $state([1, 2, 3, 4]);
3+
let total = $derived(numbers.reduce((t, n) => t + n, 0));
4+
5+
function addNumber() {
6+
numbers.push(numbers.length + 1);
7+
console.log(numbers);
8+
}
9+
</script>
10+
11+
<p>{numbers.join(' + ')} = {total}</p>
12+
13+
<button onclick={addNumber}>
14+
Add a number
15+
</button>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
let numbers = $state([1, 2, 3, 4]);
3+
let total = $derived(numbers.reduce((t, n) => t + n, 0));
4+
5+
function addNumber() {
6+
numbers.push(numbers.length + 1);
7+
}
8+
9+
$inspect(numbers).with(console.trace);
10+
</script>
11+
12+
<p>{numbers.join(' + ')} = {total}</p>
13+
14+
<button onclick={addNumber}>
15+
Add a number
16+
</button>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Inspecting state
3+
---
4+
5+
It's often useful to be able to track the value of a piece of state as it changes over time.
6+
7+
Inside the `addNumber` function, we've added a `console.log` statement. But if you click the button and open the console drawer (using the button to the right of the URL bar), you'll see a warning, and a message saying the message could not be cloned.
8+
9+
That's because `numbers` is a reactive [proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). There's a couple of things we can do. Firstly, we can create a non-reactive _snapshot_ of the state with `$state.snapshot(...)`:
10+
11+
```js
12+
/// file: App.svelte
13+
function addNumber() {
14+
numbers.push(numbers.length + 1);
15+
console.log(+++$state.snapshot(numbers)+++);
16+
}
17+
```
18+
19+
Alternatively, we can use the `$inspect` rune to automatically log a snapshot of the state whenever it changes. This code will automatically be stripped out of your production build:
20+
21+
```js
22+
/// file: App.svelte
23+
function addNumber() {
24+
numbers.push(numbers.length + 1);
25+
---console.log($state.snapshot(numbers));---
26+
}
27+
28+
+++$inspect(numbers);+++
29+
```
30+
31+
You can customise how the information is displayed by using `$inspect(...).with(fn)` — for example, you can use `console.trace` to see where the state change originated from:
32+
33+
```js
34+
/// file: App.svelte
35+
$inspect(numbers)+++.with(console.trace)+++;
36+
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)