Setting infinitely deep store?
I'm building a website builder, and my model looks like this:
Then I have a store like this:
How would I go about setting the content in the nth depth, without knowing how deep it is?
Can I do some sort of loop inside the
setPageStore
?18 Replies
You can use
produce
and do a loop in there, but you may be better off using a mutable store with createMutable
. createStore
tends to be pretty finicky when building recursive components, you just have to be careful with createMutable
to batch updates yourself as necessarycool, relatively unknown feature of solid's store is the following:
can be very handy for this type of nested, dynamic trees
it is mentioned here in the docs
Thanks for the produce suggestion, I've never heard about that! But I'm unable to find an example of using it with a loop. Do you have an example?
You could do something like:
inside the produce-callback you can mutate the store as you wish
Thanks for the example! Will play around with it a bit and see if I can get it to work for my case!
you are very welcome!
I got it to work!
But I don't know if there's a better way to do it? I've come up with this:
And then to update the content in child elements:
(could u add syntax highlighting to the snippet? add tsx after the triple backticks)
the first backticks
thank u!
Hahaha you saw that! 😄 Thx
what i am not completely getting from your code is
why not do
setStore('page', 'elements', element_indexes[0], ...)
?
pageStore.page.elements[element_indexes[0]]
this line gives me the impression that you do know the path, but mb i m misunderstanding the codeelement_indexes is to tell how deep in the element tree I'm going.
It can be: or
But I don't know which one it will be. So fo each of the indexes in the element_indexes I'll go deeper. Does that make sense?
o i see
I think there might be a cleaner way of expressing this, something like:
but i think the main idea is good: just walk that tree
lol that is so much cleaner! Knew there would be a simpler way haha thank you so much for your help, really appreciate it! 😎
you are very welcome!
often when doing recursion you can get away with doing loops too
especially w this type of nested tree stuff it can get spaghetti very fast!
Yes, especially for me as a much more visual frontend/ux designer. I'm not good at "seeing" the logical way to code. Guess I'll have to hire an Italian chef in the future to clean up all the spaghetti code I've made haha
also don't know if you already saw it, but since you mentioned you are building a website builder: https://discord.com/channels/722131463138705510/1201177657480577195
Cool! Thanks for the link to nitro! I'm taking a bit of a different approach though, building a stupid webbuilder for people that don't know what columns or paddings are haha
the more solid web builders the merrier
if you have make some progress at some point, definitely share in https://discord.com/channels/722131463138705510/734440921776783391 would love to follow up