In one of my Svelte components, I'm trying to reset the count value in the store to 0 upon mounting the component.
Here's how I'm attempting to do it:
<script> import { onMount } from "svelte"; import { writable } from 'svelte/store'; const myStore = writable({ count: 1 }); function updateCount() { $myStore.count = 0; console.log("my store:", $myStore); } onMount(() => { updateCount(); console.log("my store on mount:", $myStore); // count will be 1 });</script><!-- count is 1 --><h1>{$myStore.count}</h1>
However, upon rendering the component, I notice that the count value remains 1 instead of being updated to 0 as intended. Even though the updateCount() function is called inside the onMount hook, the console logs inside the hook show that the count is not updated.
For now to fix it I have it like this:
onMount(() => { setTimeout(() => { updateCount(); }, 1); });
Thank you for your help!