Video details

How to use Svelte store in .js file?


How to use Svelte store in .js file?
✨❓ Svelte Question Time ❓✨
"You have a Svelte question, I have an answer" 😱😍
Introduction 00:00
Svelte store is a bless in a Svelte component, you can auto-subscribe store value using $-prefixed variable, which Svelte takes care of subscribing and unsubscribing the store for you.
However, accessing store value outside of .svelte module may be a nightmare.
Here are 2 of my advices to use store in a .js file.
1. derived() or get() 01:50 whenever you want to calculate something from a store, you need to decide whether the result is going to be a snapshot value of the calculation or you need a store that will keep calculating every time when the store used for the calculation has changed.
This decides, whether to use get() to get the value at the point of time, in the former case, or use derived() to create a new store, in the latter case
2. subscribing inside the javascript file 10:24 If you keep needing to get the value of the store, you could subscribe the store and assigning it to a variable, which can be accessed from anywhere else. This saves some code of having to call get() to get the current value of the store.
🎧 Svelte Tutorial for Beginners 🎧 Svelte Question Time
Related Resources Svelte Svelte REPL
🥰 Support me
Want to learn more from me? Check out these links:

Twitter: Blog: Newsletter:
#svelte #sveltequestiontime
Thanks for watching 🥰 -- with love, Li Hau