Solid.js does not render children in a `<For>` loop correctly
Take a look at my component code:
The
props.trashIcon
is only rendering once in this loop for the last item only. How can I get it to render for every element? I've tried everything I could think of like making it a safe child and wrapping it in functions and nothing has worked. Is this possible or a bug?20 Replies
For extra information, I am using this as a framework component from within Astro, and
trashIcon
is coming as a named slot from Astro. I don't think this has anything to do with this problem, but it could.Try making
props.trashIcon
a functionTried this:
const renderTrashIcon = () => props.trashIcon;
, still results in one (the last) icon being rendered in the list.Is it possible to update the original
props.trashIcon
to be a function instead?Are you saying to make the prop passed into this component a function? So the new props type would look like this?
I haven't found a successful way to do that, as Astro is handling the prop injection. I just get a JSX-like element from the props object.
Hm okay I see, the reason I was suggesting it and what is most likely the origin of the problem is that you can't render the same html node in multiple spots.
If you don't mind a slightly messy possible solution, make a function that reads in the trash icon and manually calls
cloneNode
on it and then using that returned value in the jsx. I've never done this before but it might work for this case and should be fine since it's just an iconOh really? Hmm. May have to see if this might be a bug in the Astro community. Or if there is a certain way to accomplish this in Solid that's more "standard" when dealing with this problem.
Thanks for the suggestion. I'll give that a try now.
Darn, I get
TypeError: props.trashIcon.cloneNode is not a function
.
I feel like this is a common problem. Maybe not. I'm still digging through docs to see if there is anything said about this.what does logging
props.trashIcon
give you?It gives me a DOM element/node that looks like this:
maybe it's because of the astro slot, weird
I'll file a bug report on the Astro side. See if this is a probem with the rendering/transformation of Astro components to Solid.js components.
For posterity looking more into this problem, more info will be tracked here: https://github.com/withastro/astro/issues/12212
GitHub
[@astrojs/solid-js] Passing in children (or named slots) won't be r...
Astro Info Astro v4.15.11 Node v22.9.0 System macOS (arm64) Package Manager pnpm Output hybrid Adapter none Integrations @astrojs/solid-js @astrojs/tailwind astro-icon If this issue only occurs in ...
I think they stated this is not supported https://docs.astro.build/en/basics/astro-components/#named-slots
without actually seeing your code, you probably are using this component directly in astro, right?
using astro's named slots
I replicated this issue in this StackBlitz project here: https://stackblitz.com/edit/withastro-astro-hj9dxh?file=src%2Fcomponents%2FRenderListInSolid.tsx
yes, exactly
If I am understanding you, I think so
instead of doing that, you could just render the icon using solid instead of astro's named slots
though I'm not sure if that's even supported
That's true. It also happens with children as well. If this can't be fixed, I guess I can just render them with Solid instead.
not implying that this can't be fixed
Well thanks for both of your assistance!