Skip to content
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

[docs] Add clarification about reactivity and arrays #6547

Merged
merged 4 commits into from
Jul 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions site/content/docs/01-component-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ To change component state and trigger a re-render, just assign to a locally decl

Update expressions (`count += 1`) and property assignments (`obj.x = y`) have the same effect.

Because Svelte's reactivity is based on assignments, using array methods like `.push()` and `.splice()` won't automatically trigger updates. Options for getting around this can be found in the [tutorial](tutorial/updating-arrays-and-objects).

```sv
<script>
let count = 0;
Expand All @@ -109,6 +107,24 @@ Because Svelte's reactivity is based on assignments, using array methods like `.
</script>
```

---

Because Svelte's reactivity is based on assignments, using array methods like `.push()` and `.splice()` won't automatically trigger updates. A subsequent assignment is required to trigger the update. This and more details can also be found in the [tutorial](tutorial/updating-arrays-and-objects).

```sv
<script>
let arr = [0, 1];

function handleClick () {
// this method call does not trigger an update
arr.push(2);
// this assignment will trigger an update
// if the markup references `arr`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should go on the previous line? it's sort of a short line break. Probably fine either way though

arr = arr
}
</script>
```

#### 3. `$:` marks a statement as reactive

---
Expand Down