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: added additional explanation for valid mutation and re-assignment of $state runes. #15070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions documentation/docs/02-runes/02-$state.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ let { done, text } = todos[0];
todos[0].done = !todos[0].done;
```

### Re-assignment vs Mutation

When you are in the .svelte file, you can use re-assignments (making a variable `=` something else) or mutations to trigger re-renders .

```js
<script>
let num = $state(5);
</script>

<h1>{num}</h1>
<button onclick={() => num = 7}>change</button>
```

However, once you pass a rune into a function, you can only use mutable types like arrays and objects. Only mutation operations will trigger re-rendering of states.

```js
function inc(x) {
x++; // function only receives the value of x and not the rune, no re-rendering occurs
}

function addElem(arr) {
arr.push(1); // this triggers re-rendering
}
```
> [!NOTE] Mutation operations include .push, .splice ... where the original object is modified ad opposed to creating a new, different instance.


### Classes

You can also use `$state` in class fields (whether public or private):
Expand Down Expand Up @@ -250,3 +277,5 @@ console.log(total.value); // 7
```

...though if you find yourself writing code like that, consider using [classes](#Classes) instead.


Loading