Add clarification on how reactivity works

Based on the fact that there are multiple issues were opened related to a perceived bug on the reactive variables, I thought it would be good to add a clarification on the docs. Part of the text is taken from [this comment](https://github.com/sveltejs/svelte/issues/7818#issuecomment-1230374639) that I found super useful.
pull/7819/head
Marcos Mercuri 4 years ago committed by Marcos Mercuri
parent bfb7536c1a
commit d6e5fe7891

@ -192,6 +192,25 @@ Total: {total}
</button>
```
---
Is important to note that the reactive blocks are ordered via simple static analysis at compile time, and all the compiler looks at are the variables that are assigned to and used within the block itself, not in any functions called by them. This means that `yDependant` will not be updated in the following example:
```sv
<script>
let x = 0;
let y = 0;
const setY = (value) => {
y = value;
}
$: yDependant = y;
$: setY(x);
</script>
```
Moving the line `$: yDependant = y` bellow `$: setY(x)` will update `yDependant`.
---
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf.

Loading…
Cancel
Save