From d6e5fe78912f30c2c118960329f38f692dc53850 Mon Sep 17 00:00:00 2001 From: Marcos Mercuri Date: Mon, 29 Aug 2022 18:54:31 +0200 Subject: [PATCH] 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. --- site/content/docs/02-component-format.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/site/content/docs/02-component-format.md b/site/content/docs/02-component-format.md index 23508b5c7b..f2d1c2061a 100644 --- a/site/content/docs/02-component-format.md +++ b/site/content/docs/02-component-format.md @@ -192,6 +192,25 @@ Total: {total} ``` +--- +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 + +``` + +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.