Skip to content

Commit ce569f9

Browse files
authored
[docs] Add clarification on how reactivity works (#7819)
* 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](#7818 (comment)) that I found super useful. * Reword based on PR comments
1 parent bfb7536 commit ce569f9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

site/content/docs/02-component-format.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,25 @@ Total: {total}
192192
</button>
193193
```
194194

195+
---
196+
It 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 `yDependent` will not be updated when `x` is updated in the following example:
197+
198+
```sv
199+
<script>
200+
let x = 0;
201+
let y = 0;
202+
203+
const setY = (value) => {
204+
y = value;
205+
}
206+
207+
$: yDependent = y;
208+
$: setY(x);
209+
</script>
210+
```
211+
212+
Moving the line `$: yDependent = y` bellow `$: setY(x)` will cause `yDependent` to be updated when `x` is updated.
213+
195214
---
196215

197216
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf.

0 commit comments

Comments
 (0)