stereokai
stereokai
SSolidJS
Created by stereokai on 9/18/2024 in #support
Template doesn't react to property change in store of array of objects
Using either of the 2 methods, when I log the users store, I see that the name of the user has changed in the store object. However, the template doesn't update/react to the new name. Why is that?
// user: { name: string }

const [users, setUsers] = createStore([{ name: "some user" }]);
const [userName, setUserName] = createSignal("");

currUser = users[0];
setUserName("newName")

// Store update, method 1:
setUsers((user) => user === currUser, "name", userName());
// Store update, method 2:
setUsers(
produce((users) => {
const user = users.find((user) => user === currUser);
if (user) {
user.name = userName();
}
}),
);

// JSX:
<For each={users}>
{(user, index) => {
return (
<div>
{`${user.name}`}
</div>
);
}}
</For>
// user: { name: string }

const [users, setUsers] = createStore([{ name: "some user" }]);
const [userName, setUserName] = createSignal("");

currUser = users[0];
setUserName("newName")

// Store update, method 1:
setUsers((user) => user === currUser, "name", userName());
// Store update, method 2:
setUsers(
produce((users) => {
const user = users.find((user) => user === currUser);
if (user) {
user.name = userName();
}
}),
);

// JSX:
<For each={users}>
{(user, index) => {
return (
<div>
{`${user.name}`}
</div>
);
}}
</For>
36 replies