Skip to content

Commit 2d2e6f6

Browse files
Update tutorial part 1 (#274)
* tweaks * state * shuffle * more redirects * more * more * fixes * fix * update * more * more * more * more * fix * more * more * more * effects * add note * delete lifecycle and stores sections * Update apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/04-effects/index.md Co-authored-by: Simon H <[email protected]> * remove colon --------- Co-authored-by: Simon H <[email protected]>
1 parent 31d679a commit 2d2e6f6

File tree

140 files changed

+484
-1687
lines changed

Some content is hidden

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

140 files changed

+484
-1687
lines changed

apps/svelte.dev/content/tutorial/01-svelte/01-introduction/01-welcome-to-svelte/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ title: Welcome to Svelte
44

55
Welcome to the Svelte tutorial! This will teach you everything you need to know to easily build web applications of all sizes, with high performance and a small footprint.
66

7-
You can also consult the [API docs](https://svelte.dev/docs) and the [examples](https://svelte.dev/examples), or — if you're impatient to start hacking on your machine locally — create a project with `npm init svelte`.
7+
You can also consult the [API docs](https://svelte.dev/docs) and visit the [playground](https://svelte.dev/playground), or — if you're impatient to start hacking on your machine locally — create a project with `npx sv create`.
88

99
## What is Svelte?
1010

1111
Svelte is a tool for building web applications. Like other user interface frameworks, it allows you to build your app _declaratively_ out of components that combine markup, styles and behaviours.
1212

1313
These components are _compiled_ into small, efficient JavaScript modules that eliminate overhead traditionally associated with UI frameworks.
1414

15-
You can build your entire app with Svelte (for example, using an application framework like [SvelteKit](https://kit.svelte.dev), which this tutorial will cover), or you can add it incrementally to an existing codebase. You can also ship components as standalone packages that work anywhere.
15+
You can build your entire app with Svelte (for example, using an application framework like [SvelteKit](/docs/kit), which this tutorial will cover), or you can add it incrementally to an existing codebase. You can also ship components as standalone packages that work anywhere.
1616

1717
## How to use this tutorial
1818

apps/svelte.dev/content/tutorial/01-svelte/01-introduction/03-dynamic-attributes/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Our image is missing a `src` — let's add one:
1313

1414
That's better. But if you hover over the `<img>` in the editor, Svelte is giving us a warning:
1515

16-
> A11y: &lt;img&gt; element should have an alt attribute
16+
```
17+
`<img>` element should have an alt attribute
18+
```
1719

1820
When building web apps, it's important to make sure that they're _accessible_ to the broadest possible userbase, including people with (for example) impaired vision or motion, or people without powerful hardware or good internet connections. Accessibility (shortened to a11y) isn't always easy to get right, but Svelte will help by warning you if you write inaccessible markup.
1921

apps/svelte.dev/content/tutorial/01-svelte/01-introduction/05-nested-components/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ Add a `<script>` tag to the top of `App.svelte` that imports `Nested.svelte`...
2323

2424
Notice that even though `Nested.svelte` has a `<p>` element, the styles from `App.svelte` don't leak in.
2525

26-
> Component names are always capitalised, to distinguish them from HTML elements.
26+
> Component names are capitalised, to distinguish them from HTML elements.

apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/01-reactive-assignments/index.md

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
let count = 0;
33
44
function increment() {
5-
// event handler code goes here
5+
// TODO implement
66
}
77
</script>
88

9-
<button>
9+
<button onclick={increment}>
1010
Clicked {count}
1111
{count === 1 ? 'time' : 'times'}
1212
</button>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script>
2-
let count = 0;
2+
let count = $state(0);
33
44
function increment() {
55
count += 1;
66
}
77
</script>
88

9-
<button on:click={increment}>
9+
<button onclick={increment}>
1010
Clicked {count}
1111
{count === 1 ? 'time' : 'times'}
1212
</button>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: State
3+
---
4+
5+
At the heart of Svelte is a powerful system of _reactivity_ for keeping the DOM in sync with your application state — for example, in response to an event.
6+
7+
Make the `count` declaration reactive by wrapping the value with `$state(...)`:
8+
9+
```js
10+
/// file: App.svelte
11+
let count = +++$state(0)+++;
12+
```
13+
14+
This is called a _rune_, and it's how you tell Svelte that `count` isn't an ordinary variable. Runes look like functions, but they're not — when you use Svelte, they're part of the language itself.
15+
16+
All that's left is to implement `increment`:
17+
18+
```js
19+
/// file: App.svelte
20+
function increment() {
21+
+++count += 1;+++
22+
}
23+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let numbers = [1, 2, 3, 4];
3+
4+
function addNumber() {
5+
// TODO implement
6+
}
7+
</script>
8+
9+
<p>{numbers.join(' + ')} = ...</p>
10+
11+
<button onclick={addNumber}>
12+
Add a number
13+
</button>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let numbers = $state([1, 2, 3, 4]);
3+
4+
function addNumber() {
5+
numbers.push(numbers.length + 1);
6+
}
7+
</script>
8+
9+
<p>{numbers.join(' + ')} = ...</p>
10+
11+
<button onclick={addNumber}>
12+
Add a number
13+
</button>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Deep state
3+
---
4+
5+
As we saw in the previous exercise, state reacts to _reassignments_. But it also reacts to _mutations_ — we call this _deep reactivity_.
6+
7+
Make `numbers` a reactive array:
8+
9+
```js
10+
/// file: App.svelte
11+
let numbers = +++$state([1, 2, 3, 4])+++;
12+
```
13+
14+
Now, when we change the array...
15+
16+
```js
17+
/// file: App.svelte
18+
function addNumber() {
19+
+++numbers[numbers.length] = numbers.length + 1;+++
20+
}
21+
```
22+
23+
...the component updates. Or better still, we can `push` to the array instead:
24+
25+
```js
26+
/// file: App.svelte
27+
function addNumber() {
28+
+++numbers.push(numbers.length + 1);+++
29+
}
30+
```
31+
32+
> Deep reactivity is implemented using [proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), and mutations to the proxy do not affect the original object.

apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/02-reactive-declarations/+assets/app-b/src/lib/App.svelte

Lines changed: 0 additions & 15 deletions
This file was deleted.

apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/02-reactive-declarations/index.md

Lines changed: 0 additions & 30 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
</script>
9+
10+
<p>{numbers.join(' + ')} = {total}</p>
11+
12+
<button onclick={addNumber}>
13+
Add a number
14+
</button>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Derived state
3+
---
4+
5+
Often, you will need to _derive_ state from other state. For this, we have the `$derived` rune:
6+
7+
```js
8+
/// file: App.svelte
9+
let numbers = $state([1, 2, 3, 4]);
10+
+++let total = $derived(numbers.reduce((t, n) => t + n, 0));+++
11+
```
12+
13+
We can now use this in our markup:
14+
15+
```svelte
16+
/// file: App.svelte
17+
<p>{numbers.join(' + ')} = +++{total}+++</p>
18+
```
19+
20+
The expression inside the `$derived` declaration will be re-evaluated whenever its dependencies (in this case, just `numbers`) are updated. Unlike normal state, derived state is read-only.

apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/03-reactive-statements/+assets/app-a/src/lib/App.svelte

Lines changed: 0 additions & 12 deletions
This file was deleted.

apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/03-reactive-statements/+assets/app-b/src/lib/App.svelte

Lines changed: 0 additions & 17 deletions
This file was deleted.

apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/03-reactive-statements/index.md

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
let elapsed = $state(0);
3+
let interval = $state(1000);
4+
</script>
5+
6+
<button onclick={() => interval /= 2}>speed up</button>
7+
<button onclick={() => interval *= 2}>slow down</button>
8+
9+
<p>elapsed: {elapsed}</p>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
let elapsed = $state(0);
3+
let interval = $state(1000);
4+
5+
$effect(() => {
6+
const id = setInterval(() => {
7+
elapsed += 1;
8+
}, interval);
9+
10+
return () => {
11+
clearInterval(id);
12+
};
13+
});
14+
</script>
15+
16+
<button onclick={() => interval /= 2}>speed up</button>
17+
<button onclick={() => interval *= 2}>slow down</button>
18+
19+
<p>elapsed: {elapsed}</p>

0 commit comments

Comments
 (0)