<Index> with stores

Hello there, I'm building an application where I have a store defined in a separate non-component file which I have an array of objects being stored in there and being updated on a function. I'm mapping that store using <Index> using the setup below (openWindows is the store)
<Index each={openWindows}>{(data) => <p>component goes here</p>}</Index>
<Index each={openWindows}>{(data) => <p>component goes here</p>}</Index>
It starts out as an empty array, but when I add items to the store, only the first index in the store ends up being displayed. I'm verifying this by checking the length of openWindows
<p>{openWindows().length}</p>
<p>{openWindows().length}</p>
The count goes up, but no new elements are being displayed. Any suggestions would be great :)
14 Replies
proudparrot2
proudparrot2OP8mo ago
It's odd though, it was working earlier so I must have changed something that made it not work, but I can't find what it was
Brendonovich
Brendonovich8mo ago
out of curiosity, is there a reason you're using Index instead of For?
thetarnav
thetarnav8mo ago
If you could recreate it in playground it would help a lot. store + Index is not something people use too much so there might be a bug honestly. What happens if you replace Index with For or array.map? What happens if you remove all items again? is the old item still displayed? Also why in one example you’re calling openWindows as a function and not in the other?
proudparrot2
proudparrot2OP8mo ago
its dealing with primitives, and the solid docs tell me to use that instead of for For isn't solving it either habit of using signals i don't use stores that often I can't get it to recreate in playground, but I can share a repo if you want
peerreynders
peerreynders8mo ago
its dealing with primitives, and the solid docs tell me
This doesn't line up with your <p>component goes here</p> As I understand it that recommendation is based on the assumption that you are rendering primitive values into the DOM, not placing entire nested components; i.e. Index moves primitive values from one text node to another. For on the other hand moves the entire DOM fragment associated with that element around within the collection wrapper.
peerreynders
peerreynders8mo ago
I could be wrong Looking at the import chain src/root.tsx src/components/windowcontainer.tsx src/lib/window.ts I suspect that the store is created before the render so it isn't actually (fully?) reactive.
GitHub
why/src/lib/windows.ts at 1b5fb8db3fc6c5b1437752eb6a7cbc0d13d7367a ...
Contribute to proudparrot2/why development by creating an account on GitHub.
GitHub
why/src/root.tsx at 1b5fb8db3fc6c5b1437752eb6a7cbc0d13d7367a · prou...
Contribute to proudparrot2/why development by creating an account on GitHub.
peerreynders
peerreynders8mo ago
I can't verify that hypothesis in the playground. It seems to be good enough to use the Accessor and SetFunction under a reactive root; creation under a reactive root doesn't seem to be a requirement.
proudparrot2
proudparrot2OP8mo ago
okay yeah just tested .map and it's not functioning how I want it to
peerreynders
peerreynders8mo ago
Remove the Presence component and it starts working
proudparrot2
proudparrot2OP8mo ago
interesting thank you :thumb:
peerreynders
peerreynders8mo ago
And definitely use For in this case.
proudparrot2
proudparrot2OP8mo ago
will do
Brendonovich
Brendonovich8mo ago
By ‘primitives’ it refers to the data in the array, you have an array of objects which are not primitives. If you had an array of strings/numbers/bools Index would be more appropriate
proudparrot2
proudparrot2OP8mo ago
I see, thank you
Want results from more Discord servers?
Add your server