Search code examples
sveltesvelte-3svelte-store

Get/Read Value From Svelte Store


I want to read svelte store value at any given time. I understand subscribe method gets called when the value is updated. I want to read store even if there is no update on store.


Solution

  • Just like any other variable, you can use it in your template(markup) with $ in front of the store variable it is as simple as that!

    App.svelte

    <script>
        import { count } from './stores.js';
    </script>
    
    <h1>
        {$count}
    </h1>
    

    stores.js

    import { writable } from 'svelte/store';
    
    export const count = writable(0);