Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Svelte Store update

I’m having a hard time understanding how to update my store in the same file in which it’s defined. I have the following store definition:

export const myStore = writable({
    myList: [],
    myOtherList: []
});

In a later function, say I want to update myList to another list sourceList. I’ve looked at a bunch of the update function examples, and none make any sense to me. What I want to do is something like this (which obviously doesn’t work):

myStore.myList = sourceList

but for the life of me I can’t figure out the appropriate myStore.update() syntax.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In a Svelte file it’s just:

$myStore.myList = sourceList

Without $ syntax you could do:

myStore.update(current => ({ ...current, myList: sourceList }))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading