#understanding store setter updates

8 messages · Page 1 of 1 (latest)

quartz pewter
#

In this example:

import { produce } from "solid-js/store"

// without produce
setStore("users", 0, "username", "newUsername")
setStore("users", 0, "location", "newLocation")

// with produce
setStore(
  "users",
  0,
  produce((user) => {
    user.username = "newUsername"
    user.location = "newLocation"
  })
)

for the "without produce" case, would two dependency updates be triggered?

rich jay
#

yep, needs batch to batch updates

quartz pewter
#

Just to confirm, would it look like this?

import { batch } from "solid-js";

batch(() => {
  setStore("users", 0, "username", "newUsername");
  setStore("users", 0, "location", "newLocation");
});

Do you have a sense of which method is generally more preferred within Solid, produce or batch?

blissful hedge
#

setStore('users', 0, () => ({ username: 'newUserName', location: 'newLocation' })) will also result in 1 update

rich jay
#

yes, that's just it. setStore("users", 0, { username: "newUsername", location: "newLocation" }) is also an option as the object gets shallow merged

#

yeah, similar to what bigmistqke posted 😄

#

for 2nd question, IMO produce is way more easier as it's just regular mutation updates

#

there are awkward things with just setStore like deleting a property is done with undefined, adding/removing array elements in the middle requires splitting, merging the array