-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tutorial chapter for mutations - fixes #2243
- Loading branch information
1 parent
c5b4012
commit b176f93
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
site/content/tutorial/02-reactivity/04-updating-arrays-and-objects/app-a/App.svelte
This file contains 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,15 @@ | ||
<script> | ||
let numbers = [1, 2, 3, 4]; | ||
function addNumber() { | ||
numbers.push(numbers.length + 1); | ||
} | ||
$: sum = numbers.reduce((t, n) => t + n, 0); | ||
</script> | ||
|
||
<p>{numbers.join(' + ')} = {sum}</p> | ||
|
||
<button on:click={addNumber}> | ||
Add a number | ||
</button> |
15 changes: 15 additions & 0 deletions
15
site/content/tutorial/02-reactivity/04-updating-arrays-and-objects/app-b/App.svelte
This file contains 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,15 @@ | ||
<script> | ||
let numbers = [1, 2, 3, 4]; | ||
function addNumber() { | ||
numbers = [...numbers, numbers.length + 1]; | ||
} | ||
$: sum = numbers.reduce((t, n) => t + n, 0); | ||
</script> | ||
|
||
<p>{numbers.join(' + ')} = {sum}</p> | ||
|
||
<button on:click={addNumber}> | ||
Add a number | ||
</button> |
26 changes: 26 additions & 0 deletions
26
site/content/tutorial/02-reactivity/04-updating-arrays-and-objects/text.md
This file contains 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,26 @@ | ||
--- | ||
title: Updating arrays and objects | ||
--- | ||
|
||
Because Svelte's reactivity is triggered by assignments, using array methods like `push` and `splice` won't automatically trigger updates. For example, clicking the button doesn't do anything. | ||
|
||
One way to fix that is to add an assignment that would otherwise be redundant: | ||
|
||
```js | ||
function addNumber() { | ||
numbers.push(numbers.length + 1); | ||
numbers = numbers; | ||
} | ||
``` | ||
|
||
But there's a more *idiomatic* solution: | ||
|
||
```js | ||
function addNumber() { | ||
numbers = [...numbers, numbers.length + 1;] | ||
} | ||
``` | ||
|
||
You can use similar patterns to replace `pop`, `shift`, `unshift` and `splice`. | ||
|
||
> Assignments to *properties* of arrays and objects — e.g. `obj.foo += 1` or `array[i] = x` — work the same way as assignments to the values themselves. |