How to use Svelte store in .js file?
✨❓ Svelte Question Time ❓✨
"You have a Svelte question, I have an answer" 😱😍
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
🔗 REPL https://svelte.dev/repl/efa1988768be42328fc2ece8f1634e4c
🎧 Svelte Tutorial for Beginners https://www.youtube.com/playlist?list=PLoKaNN3BjQX3mxDEVG3oGJx2ByXnue_gR 🎧 Svelte Question Time https://www.youtube.com/playlist?list=PLoKaNN3BjQX1I9FmIoCaws4vUaoqezpc1
Related Resources Svelte https://svelte.dev Svelte REPL https://svelte.dev/repl
🥰 Support me https://www.buymeacoffee.com/lihautan
Want to learn more from me? Check out these links:
Twitter: https://twitter.com/lihautan Blog: https://lihautan.com/ Newsletter: https://lihautan.com/newsletter
Thanks for watching 🥰 -- with love, Li Hau