-
-
Notifications
You must be signed in to change notification settings - Fork 190
Update tutorial part 1 #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d06ffdb
tweaks
Rich-Harris 6414dcc
state
Rich-Harris 9403b0f
shuffle
Rich-Harris 4ce3c27
more redirects
Rich-Harris fd41c36
more
Rich-Harris 57d488a
more
Rich-Harris 6bffa15
fixes
Rich-Harris b423d1b
fix
Rich-Harris d54dfe1
update
Rich-Harris 6c550d9
more
Rich-Harris 84f2e6a
more
Rich-Harris 19fab0c
more
Rich-Harris 5ab8550
more
Rich-Harris 58e2243
fix
Rich-Harris 0c3691c
more
Rich-Harris f547b59
more
Rich-Harris 2205f8c
more
Rich-Harris 7ab75e2
effects
Rich-Harris d55a209
add note
Rich-Harris 945c8a0
delete lifecycle and stores sections
Rich-Harris 379916e
Update apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/04-ef…
Rich-Harris 16372b7
remove colon
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
...e.dev/content/tutorial/01-svelte/02-reactivity/01-reactive-assignments/index.md
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ignments/+assets/app-b/src/lib/App.svelte → ...01-state/+assets/app-b/src/lib/App.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
<script> | ||
let count = 0; | ||
let count = $state(0); | ||
|
||
function increment() { | ||
count += 1; | ||
} | ||
</script> | ||
|
||
<button on:click={increment}> | ||
<button onclick={increment}> | ||
Clicked {count} | ||
{count === 1 ? 'time' : 'times'} | ||
</button> |
23 changes: 23 additions & 0 deletions
23
apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/01-state/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: State | ||
--- | ||
|
||
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. | ||
|
||
Make the `count` declaration reactive by wrapping the value with `$state(...)`: | ||
|
||
```js | ||
/// file: App.svelte | ||
let count = +++$state(0)+++; | ||
``` | ||
|
||
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. | ||
|
||
All that's left is to implement `increment`: | ||
|
||
```js | ||
/// file: App.svelte | ||
function increment() { | ||
+++count += 1;+++ | ||
} | ||
``` |
13 changes: 13 additions & 0 deletions
13
...v/content/tutorial/01-svelte/02-reactivity/02-deep-state/+assets/app-a/src/lib/App.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let numbers = [1, 2, 3, 4]; | ||
|
||
function addNumber() { | ||
// TODO implement | ||
} | ||
</script> | ||
|
||
<p>{numbers.join(' + ')} = ...</p> | ||
|
||
<button onclick={addNumber}> | ||
Add a number | ||
</button> |
13 changes: 13 additions & 0 deletions
13
...v/content/tutorial/01-svelte/02-reactivity/02-deep-state/+assets/app-b/src/lib/App.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let numbers = $state([1, 2, 3, 4]); | ||
|
||
function addNumber() { | ||
numbers.push(numbers.length + 1); | ||
} | ||
</script> | ||
|
||
<p>{numbers.join(' + ')} = ...</p> | ||
|
||
<button onclick={addNumber}> | ||
Add a number | ||
</button> |
32 changes: 32 additions & 0 deletions
32
apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/02-deep-state/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Deep state | ||
--- | ||
|
||
As we saw in the previous exercise, state reacts to _reassignments_. But it also reacts to _mutations_ — we call this _deep reactivity_. | ||
|
||
Make `numbers` a reactive array: | ||
|
||
```js | ||
/// file: App.svelte | ||
let numbers = +++$state([1, 2, 3, 4])+++; | ||
``` | ||
|
||
Now, when we change the array... | ||
|
||
```js | ||
/// file: App.svelte | ||
function addNumber() { | ||
+++numbers[numbers.length] = numbers.length + 1;+++ | ||
} | ||
``` | ||
|
||
...the component updates. Or better still, we can `push` to the array instead: | ||
|
||
```js | ||
/// file: App.svelte | ||
function addNumber() { | ||
+++numbers.push(numbers.length + 1);+++ | ||
} | ||
``` | ||
|
||
> 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. |
15 changes: 0 additions & 15 deletions
15
...utorial/01-svelte/02-reactivity/02-reactive-declarations/+assets/app-b/src/lib/App.svelte
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
....dev/content/tutorial/01-svelte/02-reactivity/02-reactive-declarations/index.md
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
...ontent/tutorial/01-svelte/02-reactivity/03-derived-state/+assets/app-b/src/lib/App.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script> | ||
let numbers = $state([1, 2, 3, 4]); | ||
let total = $derived(numbers.reduce((t, n) => t + n, 0)); | ||
|
||
function addNumber() { | ||
numbers.push(numbers.length + 1); | ||
} | ||
</script> | ||
|
||
<p>{numbers.join(' + ')} = {total}</p> | ||
|
||
<button onclick={addNumber}> | ||
Add a number | ||
</button> |
20 changes: 20 additions & 0 deletions
20
apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/03-derived-state/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Derived state | ||
--- | ||
|
||
Often, you will need to _derive_ state from other state. For this, we have the `$derived` rune: | ||
|
||
```js | ||
/// file: App.svelte | ||
let numbers = $state([1, 2, 3, 4]); | ||
+++let total = $derived(numbers.reduce((t, n) => t + n, 0));+++ | ||
``` | ||
|
||
We can now use this in our markup: | ||
|
||
```svelte | ||
/// file: App.svelte | ||
<p>{numbers.join(' + ')} = +++{total}+++</p> | ||
``` | ||
|
||
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. |
12 changes: 0 additions & 12 deletions
12
.../tutorial/01-svelte/02-reactivity/03-reactive-statements/+assets/app-a/src/lib/App.svelte
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
.../tutorial/01-svelte/02-reactivity/03-reactive-statements/+assets/app-b/src/lib/App.svelte
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
...te.dev/content/tutorial/01-svelte/02-reactivity/03-reactive-statements/index.md
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
....dev/content/tutorial/01-svelte/02-reactivity/04-effects/+assets/app-a/src/lib/App.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script> | ||
let elapsed = $state(0); | ||
let interval = $state(1000); | ||
</script> | ||
|
||
<button onclick={() => interval /= 2}>speed up</button> | ||
<button onclick={() => interval *= 2}>slow down</button> | ||
|
||
<p>elapsed: {elapsed}</p> |
19 changes: 19 additions & 0 deletions
19
....dev/content/tutorial/01-svelte/02-reactivity/04-effects/+assets/app-b/src/lib/App.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script> | ||
let elapsed = $state(0); | ||
let interval = $state(1000); | ||
|
||
$effect(() => { | ||
const id = setInterval(() => { | ||
elapsed += 1; | ||
}, interval); | ||
|
||
return () => { | ||
clearInterval(id); | ||
}; | ||
}); | ||
</script> | ||
|
||
<button onclick={() => interval /= 2}>speed up</button> | ||
<button onclick={() => interval *= 2}>slow down</button> | ||
|
||
<p>elapsed: {elapsed}</p> |
46 changes: 46 additions & 0 deletions
46
apps/svelte.dev/content/tutorial/01-svelte/02-reactivity/04-effects/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Effects | ||
--- | ||
|
||
So far we've talked about reactivity in terms of state. But that's only half of the equation — state is only reactive if something is _reacting_ to it, otherwise it's just a sparkling variable. | ||
|
||
The thing that reacts is called an _effect_. You've already encountered effects — the ones that Svelte creates on your behalf to update the DOM in response to state changes — but you can also create your own with the `$effect` rune. | ||
|
||
> Most of the time, you shouldn't. `$effect` is best thought of as an escape hatch, rather than something to use frequently. If you can put your side effects in an [event handler](dom-events), for example, that's almost always preferable. | ||
|
||
Let's say we want to use `setInterval` to keep track of how long the component has been mounted. Create the effect: | ||
|
||
```svelte | ||
/// file: App.svelte | ||
<script> | ||
let elapsed = $state(0); | ||
let interval = $state(1000); | ||
|
||
+++ $effect(() => { | ||
setInterval(() => { | ||
elapsed += 1; | ||
}, interval); | ||
});+++ | ||
</script> | ||
``` | ||
|
||
Click the 'speed up' button a few times and notice that `elapsed` ticks up faster, because we're calling `setInterval` each time `interval` gets smaller. | ||
|
||
If we then click the 'slow down' button... well, it doesn't work. That's because we're not clearing out the old intervals when the effect updates. We can fix that by returning a cleanup function: | ||
|
||
```js | ||
/// file: App.svelte | ||
$effect(() => { | ||
+++const id =+++ setInterval(() => { | ||
elapsed += 1; | ||
}, interval); | ||
|
||
+++ return () => { | ||
clearInterval(id); | ||
};+++ | ||
}); | ||
``` | ||
|
||
The cleanup function is called immediately before the effect function re-runs when `interval` changes, and also when the component is destroyed. (If the effect function doesn't read any state when it runs, it will only run once, when the component mounts.) | ||
|
||
> Effects do not run during server-side rendering. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.