sveltejs / sites

Monorepo for the sites in the Svelte ecosystem
https://svelte.dev
MIT License
286 stars 123 forks source link

[suggestion] talk about mutation in a writable store that is an object #458

Open ZYinMD opened 1 year ago

ZYinMD commented 1 year ago

I was learning svelte by reading the interactive tutorial from start to finish. When I got to the readable store part, having used redux, I immediately think:

  1. Nice, I can use writable stores as mini redux stores.
  2. Can I mutate it? like this:
const myStore = writable({ foo: 1 });

myStore.update((prev) => {
  prev.foo = 2;
  return prev;
});

The answer is yes, and that's a HUGE deal, because you can't do it in redux, and that has been drilled to people's heads.

In the current tutorial here and here, the example store is an integer:

export const count = writable(0);

And my initial assumption was "I probably can't do mutate in a store, otherwise the doc wouldn't have used an integer to demo it". Many many others may think like me - being able to mutate feels too good to be true.

So, I suggest adding a demo of an object as writable store. You have no idea how much redux has changed people brain.

(side note: even chatGPT is affected by redux, try asking I have a svelte writable store that is an object, how do I update a slice of it? and see what it says)