I'm new to Svelte Store. Here in svelte tutorial, they used update()
method in <Incrementer/>
and <Decrementer/>
components to update value. But in <Resetter/>
, they used set()
method to reset value. What exactly is the difference between update()
and set()
method in svelte store?
From the docs:
set
is a method that takes one argument which is the value to be set. The store value gets set to the value of the argument if the store value is not already equal to it.
update
is a method that takes one argument which is a callback. The callback takes the existing store value as its argument and returns the new value to be set to the store.
So you can use update
if the next value should be dependent on the current value.
Also: update
only exists for convenience and is not part of the "store contract":
subscribe
methodset
methodComponent code like $store = value
uses set
internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With