Reactive Store for a For html element?

Hi I have created a store using createStore but when I access it in a for element it doesnt update reactive. How can I do that?
9 Replies
Greenman999
Greenman999OP4mo ago
The store is a list of user objects I want the html (<For each={users}><For/>} to update when e.g. user[0].online updates okay now I did it with the accessor of the index <For each={users}>{(user, i) => {}}<For/> but is there a better way?
zulu
zulu4mo ago
you need to show the code that didn't work
Greenman999
Greenman999OP4mo ago
const [users, setUsers] = createStore<User[]>([]);

type User = {
id: string;
name: string;
answer: string;
online: boolean;
};

return <For each={users} fallback={<p>Loading...</p>}>
{(user) => (
<p>{user.online}<p/>
)}
</For>
const [users, setUsers] = createStore<User[]>([]);

type User = {
id: string;
name: string;
answer: string;
online: boolean;
};

return <For each={users} fallback={<p>Loading...</p>}>
{(user) => (
<p>{user.online}<p/>
)}
</For>
zulu
zulu4mo ago
this should work, how do you update the users?
Greenman999
Greenman999OP4mo ago
setUsers(
(user, i) => user.id === data.user_id,
"online",
data.content
);
setUsers(
(user, i) => user.id === data.user_id,
"online",
data.content
);
data is just some object with content being a boolean
zulu
zulu4mo ago
sure this also should work can you try minimal repro in https://playground.solidjs.com/
Greenman999
Greenman999OP4mo ago
on it hmm it seems to work in the playground
zulu
zulu4mo ago
ha, that is why I said it looks like this should work anything else you think you are missing?
Greenman999
Greenman999OP4mo ago
I just updated my code just like in my playground and now it works lol I don't know what was wrong ty

Did you find this page helpful?