Is there a way to access children props?
Ih this code:
I was hoping to be able to pass the children props to the dynamic component. Is there a way to do this?
31 Replies
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
the idea would be to pass the children props here:
you can do it with a WeakMap: https://playground.solidjs.com/anonymous/06bf6c6c-3177-43c4-9680-30f9239f9f55
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
π€―
this is insanely complex. will have to digest that. thank you though ;D
ok its readable
i wonder how clean this is
ye it's a bit sneaky π
^^
it would be more readable with a ref
but then it would be undefined when getting it from the WeakMap
I believe the original need is sneaky as well and could be solved in another manner
but TIL
u could also return non-dom elements
exactly
that is how
<Route/>
works in solid-router
the other way around. much cleaner
but the WeakMap is maybe less hacky at the end of the day
what do you mean?
it was a good and valid use case for WeakMap ;D
i meant having the props defined and just loop on it. probably dozen of solutions to avoid such cryptic solution. however I applaud the way of thinking outside the box π
ty π
you are very welcome!
i love these kind of sneaky stuff π
XD
solid's jsx is so flexible
u can really do whatever
yeah i forgot that it is.
the fact you can do vanilla js so easily inside of solid is awesome
exactly, a div is a div π
What about storing the props in a symbol property in the element itself?
That way you have direct access to it.
can you extend on that? im not sure what your suggestion is π
I think I should ask first if you are familiar with the
Symbol
primitive value of JS to make sure we are on the same page.Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Or you can omit Dynamic component altogether and make Line return accessor instead ( we are not unnecessarily recreating Lines html node and then ignoring it and sneaking original props around but directly passing new constructor): https://playground.solidjs.com/anonymous/40015254-f106-4a3f-ab38-e62cd7018fa9
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Or forward everything to Editor and let him do the heavy lifting: https://playground.solidjs.com/anonymous/c4d4a3e4-48ec-46a1-ad5f-18d9b415f707
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
If it is just about numbering, you can do this with css and save all the trouble:
https://blog.logrocket.com/styling-numbered-lists-with-css-counters/
Otherwise I usually create a context on the parent (the Editor in this case) and provide a map to which each child can add itself (its ref) onMount and remove itself onCleanup.
And the component can then determine its index by himself by finding itself in the map.
This is how component libraries like kobalte.dev do it as well.
Supun Kavinda
LogRocket Blog
Styling numbered lists with CSS counters - LogRocket Blog
With CSS counters, you can customize the appearance of the numbers in an ordered list without adding more elements to the DOM.
Otherwise I usually create a context on the parent (the Editor in this case) and provide a map to which each child can add itself (its ref) onMount and remove itself onCleanup.Doesn't this fail when there is conditional rendering and you count on order of the children? Or swapping order of elements with
<For/>
Thatβs right. If you render components conditionally: yes
Thatβs why the css solution is so great.π
But you can sort the items before setting:
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
TIL about
compareDocumentPosition
and Node.DOCUMENT_POSITION_PRECEDING
and the like. Node.DOCUMENT_POSITION_PRECEDING
is like a global of the current comparison? strange api!lots of valid suggestion, brains are cooking here ;D