Reputation: 2714
The following code does not run when I update the window.test
variable, I do not have any log displaying on my console. I thought that test()
would be run. Can't we make global variable reactive using svelte ?
$: window.test && test();
function test() {
console.log('success');
}
Upvotes: 2
Views: 2889
Reputation: 13
With the new Svelte version 5, it is also possible to work with reactive variables independently of the Svelte context via runes. Find out more about Runes in the blog post.
However, the answer remains the same, it must always run via a store, or here via the runes.
Keep in mind, the file name must have the following syntax so that the Svelte compiler can react to it: name.svelte.ts
Upvotes: 0
Reputation: 29897
No, we can't make global (or imported) variables reactive.
Svelte's reactivity for assignments is restricted to local variables inside *.svelte files.
The recommended method to enable reactivity in other places is by creating stores, either by using svelte/store directly or by using objects that fulfill the Store contract.
Upvotes: 3