Defining the Svelte store
To understand why Svelte reactivity is limited within a Svelte component, we must first understand how Svelte’s reactivity works.
Unlike some other frameworks, Svelte reactivity works during build time. As Svelte compiles a Svelte component into JavaScript, Svelte looks at each variable and tracks the variable to see when the variable changes.
Instead of tracking all the variables throughout the application, Svelte limits itself to only analyzing and compiling one file at a time. This allows Svelte to compile multiple Svelte component files in parallel but also means that a Svelte component would not be aware of variable changes that happen in other files.
A common situation where a variable change is not tracked is when the variable is defined in a separate file and imported into the current component.
In the following code snippet, the quantity
variable is imported from a separate file. Svelte will not track any changes to the quantity...