S
SolidJS•10mo ago
ndyg

understanding store setter updates

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"
})
)
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?
5 Replies
mdynnl
mdynnl•10mo ago
yep, needs batch to batch updates
ndyg
ndygOP•10mo ago
Just to confirm, would it look like this?
import { batch } from "solid-js";

batch(() => {
setStore("users", 0, "username", "newUsername");
setStore("users", 0, "location", "newLocation");
});
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?
bigmistqke
bigmistqke•10mo ago
setStore('users', 0, () => ({ username: 'newUserName', location: 'newLocation' })) will also result in 1 update
mdynnl
mdynnl•10mo ago
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
ndyg
ndygOP•10mo ago
I appreciate the advice 💚
Want results from more Discord servers?
Add your server