Set a value to a Svelte 3 store

InstructorTomasz Łakomy

Share this video with your friends

Send Tweet

Sometimes we don't want to have a complicated update function to change a value of our store. We know exactly what we want to change the value to - we can just use the store.set function to do that.

In this lesson we're going to learn how to set a value to a Svelte 3 store by using the store.set function to set the value of a counter without having to increment/decrement it one by one.

Krit
~ 5 years ago

What's the difference between store.update and store.set?

I guess only different is update also pass down the current state.

Tomasz Łakomyinstructor
~ 5 years ago

Thanks for asking!

‘store.update’ takes an update function as an argument and ‘store.set’ just sets the store value to whatever you provide to it.

~ 3 years ago

My code was malfunctioning when I clicked the increment button immediately after setting the store value via the input element. The increment store.update function (n=> n + 1) was interpreting the value as a string and appending 1 to the number. eg set value = 20, increment => 201, increment => 2011. Fixed it by adding parseInt to the Setter set function => set(parseInt(event.target.value))