Reactive conditional rendering based on Store of objects
Heres an example of what I am trying to do:
I have two Stores, containing Arrays of the same object.
When displaying each object of one Store, I want to format the item based on whether or not the object can be found in the other Store. This is simple enough.
However, when I update the second Store, I want the formatting on the displayed items to update reactively. So in this example, when you press the button to add the "foo" tag, the "foo" item printed above should become bold.
Thank you for your assistance.
(I know it would be easier, in this example, to just have a single Store and an "enabled" field on the Tag, however in my actual application, the above structure is necessary.)
23 Replies
Can we let everything live within 1 store?
Unfortunately not
To know which is applied, we just need to have a boolean value
At least not in a way I can think of. You see, in my actual application, there are many things that take tags, and the tags for each item are stored separately from the main Store of tags.
So the tags are locally stored? And not from server?
:Worry_Think:
Never thought about this, but this is what came to my mind
Making the tag in the applied store as a getter instead (a function)
How do you mean?
But this seems too smelly when thinking in the solidjs way
:_Worry_Cuoituthien:
No, tags are stored on the server. This example is dramatically simplified
So you need to have some sort of revalidation right?
I think there should be an API to query a single tag
Then you can cache it as you want
But if there's none, I think we need another way
:Worry_Think:
Like setAppliedTag and the value is a function
But the best seems to be just using one global store
:Worry_Think:
You can write to the store with createComputed to initialize the store
Solidjs store is really powerful and not complicated at all
My application works like this:
- I send a request to the server for all tags, and put them in a store.
- For a given item, I query the server for the tags applied to that item, and put those tags into another store.
- I display all tags, with the applied tags highlighted so that the user can see which tags are applied to the item.
When i change the tags applied to an item, I want the display to update reactively
Then if you need to display, go with a loop through keys and access
Set applied is basically modifying the array of tags inside corresponding item
:Worry_WOW: I think the store can have the ability to cover a whole sql table
I dont think i'm following what you're saying. I refactored the above example and it doesn't work:
Is this what you meant?
Which part of it doesn't work?
Solid also has their Conditional Rendering components like <Show> so you don't need to use ternary operator
For appending value to array, see store guide
https://docs.solidjs.com/concepts/stores
The desired behavior doesn't happen. Adding the new applied tag does not update the formatting of of the displayed list
Can you try logging the store as you appended? If the store's values are updated, then there was something wrong with the rendering
Not the store logic
it has something to do with the ternary
if you use
<Show/>
your code works: https://playground.solidjs.com/anonymous/6af2db26-5d4c-4a9e-a997-7c3f47abc134Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
a i found the reason why it doesn't work: you are returning the ternary directly inside
<For/>
returning directly (your snippet)
wrapping it in a fragment
<></>
it's not so clear when you return it directly, but when you write it a bit differently it becomes obvious why your original snippet is not reactive: it accesses the store inside the component-body
another good reason why to use control flow components in solidjs, was a tricky to spot bug
Thank you for the solution and the detailed explanation 👍